[SOLVED] Remove entity in an array


(John Andersson) #1

In short, how to not only remove an entity from an array, but also remove it from the world?


(Martí Angelats i Ribera) #2

Recycle the entity like it wasn’t in the array and delete it from the array.

AS3 works with pointers so you won’t have any problem.


(John Andersson) #3

The thing is that the only way I am keeping track of the entities is through the array, since in my game if you spawn more than 10 soldiers, the first soldier dies, then the second etc. So they get replaced. I thought the best way of dealing with that was with an array


(Martí Angelats i Ribera) #4

What? Do you know how the recycle system works? Also take a look how AS3 garbage collctor works.

The idea is that the recycle system still keeps track of it. You have to stop to track it. So simply Recycle the Entity (like you would do in a normal situation) and then remove it from the array.


(John Andersson) #5

Oh, sorry, I meant remove :stuck_out_tongue:

Here is the code I use:

var soldier:Soldier = new Soldier(x + 50, y + 50);

if(input.pressed(Key.W))
{
    soldier.creator = this;
    FP.world.add(soldier);
    soldierArray.push(soldier);

    //I want to add something like this:
    if(soldierArray.length > 10) remove first entry in the array from the world and array
}

(John Andersson) #6

Okay, I found the solution. I tried this earlier but I must have misspelled something

if (soldierArray.length >= 10)
    {
	var soldierToRemove:Soldier;
	soldierToRemove = soldierArray.shift()
        FP.world.remove(soldierToRemove);
}

(Bora Kasap) #7

Why not?

FP.world.remove(soldierArray.shift());