PDA

View Full Version : Deleting an object from array



superpeppe
July 24th, 2007, 06:37 AM
Hi

I am currently developing a game in AS3. The game has an array that contain all Movable objects, but during the game a Movable might die, so the question is how do i delete that array element? There is of course the splice() Method, but does it delete the object or what? I searched the internet, and some places the write that I should use the delete statement, setting a null reference or something completely different...

By the way it would be nice if you could post the code, it's a lot easier to understand:thumb:

Thanks in advance!

CarlLooper
July 24th, 2007, 06:58 AM
An array of objects is actually an array of references to such objects. When you remove a reference from the array (eg. using splice) the object itself is not removed. The best thing to do is to:

1. depending on the type of object, eg. if it is a display object on the display list you'll want to remove it from the display list
2. null the array reference eg. arr[i]=null;
3. remove the reference from the array with splice
4. if any, null any other references to the object that you might have created.

Eventually, behind the scenes, the object itself will be removed from memory (called garbage collection).

Carl

Aquilonian
July 24th, 2007, 07:34 AM
You will never know when the GC will remove it, so its also good to make a function to unactivate it, i mean, remove all the listeners,remove anything its doing each frame or you will have unexpected results

Charleh
July 24th, 2007, 08:06 AM
Yeah GC is reliable, but your code needs to be too, you need to make sure all references to the object are GONE - so try not to add references to objects in global scope...!