How do you create unique instance variables?


(Blake Jones) #1

So i’m pretty new to all this and I was thinking …

if I had a bunch of enemies running around the screen each one would need to have its own unique name if I wanted to kill one without it despawning all of them in the entire game…this is my code attempting to do that

var index:int = 1;

for each (shroomDude in list) 
{  										   										
var s:String = index.toString();
// add it
var shroomDude[s]:shroomDudeEntity = new shroomDudeEntity(shroomDude.@x,shroomDude.@y));
index++;
}

but i get the error “expecting semicolon before left bracket”. am I using the brackets wrong? and is this the best way to do this? or is there another way to access a colliding entity without forcing it to have a unique name


(Alex Larioza) #2

Your declaration of an array is in correct. Here’s the correct method:

var shroomDudes:Array = new Array();
var i:int = 0
for each (shroomDude in list) 
{                                                                               
    // add it
    shroomDude[i] = new shroomDudeEntity(shroomDude.@x,shroomDude.@y);
    index++;
}

Its also worth noting that Entities in FlashPunk have a name property that can be used for referencing specific instances.


(David Williams) #3

When are they being removed? If you have something in the player class where you land on their heads and it removes them (think Mario), you could do something like:

var shroomDude:shroomDudeEntity = collide("shroomType", x, y+3) as shroomDudeType;
if(shroomDude)
{
    world.remove(shroomDude); 
    //removes specific instance that is being 
    //collided with 3 pixels below the player's hitbox.
}

(Blake Jones) #4

@SHiLLySiT actually it was an xml element not an array. I’ll use an array to name them though thanks for that! also: how would I give them a unique name? by parsing the name to the class when I add the instance?

@Deamonr awesome I can use this too!

both of you dont even know how much you just helped me :slight_smile:


(Blake Jones) #5

So I did this:

for each (shroomDude in shroomdudelist) 
{
var s:String = i.toString();
add(new shroomDudeEntity(shroomDude.@x, shroomDude.@y, s));
i++;
}

parsing the name to the class worked :slight_smile: they each have their own name now!


(Zachary Lewis) #6

If the array will only be made of ShroomDudeEntitys, you might want to use a Vector. They’re faster for an array of a single type.

var dudes:Vector.<ShroomDudeEntity> = new Vector.<ShroomDudeEntity>();

You can use this just like an array, except all the items are automatically read as ShroomDudeEntitys.


(Blake Jones) #7

could I do that with an xml list? would I need to turn the data into a string first?