[SOLVED] getInstance(String) question


(Bora Kasap) #1

I have instances like enemy1, enemy2 and enemy3 and more… Each one of them have a “public takedamage()” function. I have named them with “name” property.

How can i reach them in a loop?

for (var i:Number = 0; i < enemycount; i++)
				{
				world.getInstance("enemy"+i.toString()).takedamage();
				}

Debugger result:

[Fault] exception, information=TypeError: Error #1010: A term is undefined and has no properties.

And debugger is not showing me which part of code is wrong…

What i need to do to make what i want?


(Bora Kasap) #2

Problem solved with this information:

The most important thing everybody have to learn first!


(rostok) #3

Have you tried setting type to “enemy” and then this?

var a:Vector.<Entityt> = new Vector.<Entity>;
world.getType("enemy", a);
for each (var e:Enemy in a) {
...
}

(Bora Kasap) #4

no, i have just learning basics. thanks but how it works? what is the vector’s function here?


(Axel) #5

Vectors are (in case you don’t know), like arrays that can only store objects of a certain type. The vector “a” can in rostoks example, only store entities. The getType functions, then gets all entities of the type “enemy” and puts then in the “a” vector. Then you can use the for loop, to loop through all the enemies.


(Bora Kasap) #6

so that means i can’t store entities in array object, i should use a vector for to do this


(rostok) #7

no, Vector and Array are alike yet there are some differences in time for adding and fetching elements. further reading http://www.zombieflambe.com/as3/as3-dictionary-class-array-object-benchmark/

you should check out the implementation to get the clearer picture https://github.com/Draknek/FlashPunk/blob/master/net/flashpunk/World.as#L845