spritehaxehaxeflixelflixel

Make a FlxSprite behave as the child of another FlxSprite


In Flixel, you cannot add a FlxSprite to another one, like you could with Flash API (Sprites were subclasses of DisplayObject). So if you want 2 Sprites to behave like parent and child, you still have to animate them separately. This can become a nightmare if you use Tweens too.

For example, imagine a rotating spaceship that has attached thrusters, or a moving character that wears an armor, hat, shield etc.

Is there a way to have a 'child' Sprite acting like it was added into a 'parent' one, so that it updates automatically its position, scale and rotation accordingly? For example, during their FlxGroup's update() function?

I'm interested in HaxeFlixel 3.3.1, although it doesn't really matter, as this applies for all versions and ports of Flixel.

Edit: I noticed that HaxeFlixel features FlxSpriteGroup, which is supposed to handle the multiple Sprites. But this is a new feature, and I'm pretty sure that flixel developers are using different approaches for the rest of the flixel ports.


Solution

  • HaxeFlixel provides FlxSpriteGroup and FlxNestedSprite which both can be used to have some sprites behaving as a group. In both approaches, I had a problem updating the angle and scale properties, however updating the position seems to work fine.

    If you are not interested to animate the sprites separately, and painting one sprite over another is what you're after, FlxSprite's stamp() function could be what you need (e.g. draw a helmet over your character)