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
-
Okay, I'm still getting the ropes of it both Haxe itself and HaxeFlixel, so things are still a bit confusing: Can I create an array like the following? Then using that array, can I add instances of that class into it using this in a function? var id:Int = testArray.length();
testArray.push(new testClass(id));
And even if I can, am I still able to remove, kill, or manipulate those instances from the current state and array? for (i in 0...testArray.length())
{
testArray[i].testFunction(); //does this even do anything?
trace(testArray[i].id); //can this work?
testArray[i].alpha = 100; //will this change the alpha on an individual instance of testClass?
testArray[i].kill(); //does this kill only an individual instance of testClass?
} Can I manipulate each individual instance of "testClass" in these sorts of ways during runtime? Forgive me if my syntax/writing is total garbage, I'm still learning. Have some mercy. The overarching plan is to have multiple copies of the same exact sprite created and placed into the world, but each one is still unique enough that it can be deleted/manipulated individually or in mass. Thanks in advance if you can help~! |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 3 replies
-
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 value like so for (sprite in Constants.spriteNames) {
final spriteClass = Type.createInstance(sprite, []); // you can put class arguments in the empty array
// Then all the stuff you want to do should work
spriteClass.testFunction();
trace(spriteClass.id);
spriteClass.alpha = 100;
spriteClass.kill();
} |
Beta Was this translation helpful? Give feedback.
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:
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 …