Remove Sprite and Array

If I have an array to store the sprite in the LayerManager,
for example: c[0], c[1]…c[10],
after I remove Sprite c[2] in the LayerManager, will c[3] automatically become c[2]?
If not, how can I make c[3] to become c[2] after c[2] had been removed?

Thank you very much!!

The LayerManager doesn’t know about your array, so removing the sprite from the LayerManager won’t have any effect on your array.

If you just do ‘c[2] = null;’, then c[3] won’t automatically become c[2], because null is a perfectly valid value for c[2].

What you could do is (where i = 2 in your case):
System.arraycopy(c, i+1, c, i, c.length - 1 - i);
but notice that afterwards c.length won’t have changed, even though the last entry is now meaningless. You’ll have to keep a separate variable called ‘cLength’ or something and decrease it by one.

A much simpler solution is just to use a Vector instead of an array. Then, yes, if you do ‘vector.removeElementAt(2);’, afterwards the elements at 3 upwards are moved to being at 2 upwards. (Actually, the Vector’s implementation has done a System.arraycopy just as above).

[quote]A much simpler solution is just to use a Vector instead of an array. Then, yes, if you do ‘vector.removeElementAt(2);’, afterwards the elements at 3 upwards are moved to being at 2 upwards. (Actually, the Vector’s implementation has done a System.arraycopy just as above).
[/quote]
i am using this method as described above, but when i call remove element some of the 5 sprites (DROIDS) crash and the sprite stays on the screen!?

monkey = new Monkey(this);
layerManager.append(monkey);

//DROIDS
for (int i = 0; i < NUM_DROID; ++i)
{
try{
System.out.println(NUM_DROID);
Droid newDroid = new Droid(this);
layerManager.append(newDroid);
droid.addElement(newDroid);
}
catch(IllegalArgumentException e)
{System.out.println(“Error” + e);
}
}

any ideas as to what else may be necessery to handle this implementation?

many thanks!

Did you forget to also remove your Sprite from the LayerManager? Remember that your LayerManager doesn’t know about your Vector, and your Vector doen’t know about your LayerManager - it’s up to you to keep them in sync.

You should remove the Sprites like this:


droid.removeElement(deadDroid);
layerManager.remove(deadDroid);