How can I add two different classes in a single Array?

  • I have two classes, the Player class and the Enemy class, both of them extend from FlxSprite obviously and both have a public variable called "CT" that I want to access through a for loop.
    The problem is that, if I add them to a FlxGroup or FlxSpriteGroup, they are considered FlxBasic or FlxSprites respectively and so the CT is nonexistent.
    Another doubt I have is, if it's possible to add two different classes in a single Array?
    Any solution is appreciated, thank you!

  • the way I see it you have two solutions
    either makke a base class that has whatever common fields you have and make them both extend it. and when creating a FlxGroup just set the type to it.

    or you can try casting into your classess in the for loop. which will allow you to access their properties easily.

    the first solution is a lot cleaner though

  • Thank you alihassan, i was thinking of a similar solution but wanted an alternative one. I guess I'll have to go with that one!

Log in to reply

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