-
Notifications
You must be signed in to change notification settings - Fork 0
Linking sprites
Steve Butler edited this page Sep 21, 2026
·
2 revisions
A sprite can be configured to follow another sprite by adding it to a sprite's followers. This is achieved by calling the addFollower method of the Sprite class.
In the following code a text sprite is created which follows an image sprite. The following objects are presumed to have been previously initialised in the code:
-
gameAreais the main GameArea instance -
textureManageris an instance of TextureManager -
animatoris an instance of Animator.
textureManager.spriteFactory = new hcjeLib.sprites.DomImageSpriteFactory(gameArea);
const sprite = textureManager.createSprite("rocket.png",
[{name:"flying", interval: 100, cycleType: hcjeLib.sprites.CycleType.OSCILLATE}]);
sprite.dynamics = new hcjeLib.sprites.Dynamics();
const labelSprite = hcjeLib.sprites.createTextSprite(gameArea, `Rocket`, {
dimensions: {width: sprite.bounds.width, height: 0},
markdown: false
});
sprite.addFollower(labelSprite);
animator.addTarget(sprite);Important
- The container for the label sprite is set to the same container,
gameAreaas the textureManager'sspriteFactory. This is important to ensure the follower is positioned correctly relative to the leading sprite. - Only the leading sprite is added to the animator. The leading sprite automatically updates its followers.