Make FlxText into a FlxNestedSprite

  • Hello,

    I'm trying to create a version of FlxText that I can use as a nested sprite. Since FlxText already inherits from FlxSprite, it seems like this should be easy. However, I'm not sure how to do it. A bad way to do it would be to copy everything from FlxText.hx and have it extend FlxNestedSprite instead of FlxSprite. Is there a way to simply cast a FlxSprite as a FlxNestedSprite?


  • administrators

    No, there's no simple way of doing that due to how inheritance works. And the hierarchy in this case looks like this:

                      /    \
                     /      \
                    /        \
                FlxText  FlxNestedSprite

    There's no way to join those two branches together again, since Haxe (and most other languages that support OOP) don't support multiple inheritance.

    What you could try is have a FlxNestedText that extends FlxNestedSprite, but has a reference to a FlxText instance (and override some FlxNestedSprite methods to call FlxText methods instead in relevant places). It's not a pretty solution either, and it's similar to what FlxSpriteGroup does (inherits from FlxSprite, but has a FlxTypedGroup<FlxSprite> internally).

  • Thanks, that's what I figured. I had hoped there was a simple workaround since the FlxSprite and FlxNestedSprite APIs are so similar.

Log in to reply

Looks like your connection to HaxeFlixel was lost, please wait while we try to reconnect.