Skip to content

Is it possible to have an array of a specific class, and can I still manipulate each instance of that class, while it's in that array? #2400

Answered by RichardBray
GrimyKimu asked this question in Q&A
Discussion options

You must be logged in to vote

Yes, this is 100% possible. I've done this with a Map in Haxe but I'm sure it can be done with an array.

Let's say you have three sprites, (SpriteOne, SpriteTwo, SpriteTree), you have them all in a folder called 'spritesLove', and you want to loop over them. You could create a constant like this:

class Constants {
        ...
        public static final spriteNames:Map<String, Class<FlxSprite>> = [
		"Sprite1" => spritesLove.SpriteOne, 
		"Sprite2" => spritesLove.SpriteTwo,
		"Sprite3" => spritesLove.SpriteThree,		
	];
}

The strings on the left of the map can have whatever name you want. I personally use a map for save data reasons, so that I just save the string, 'Sprite2', and grab the …

Replies: 1 comment 3 replies

Comment options

You must be logged in to vote
3 replies
@GrimyKimu
Comment options

@Gama11
Comment options

@RichardBray
Comment options

Answer selected by GrimyKimu
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
3 participants