Problems in Flash #04: Deleting Dynamic MovieClips

POSTED IN Actionscript, Flash Platform | TAGS : , August 2, 2008

Anyone who’s every tried to create movieclips dynamically using the getNextHighestDepth function knows that when trying to delete that very same movie with removeMovieClip, it just simply does not work.

Senocular has a pretty nice explanation on how Zones of Depth work within flash, but it still doesn’t explain why the developer of this product decided it would be an awesome idea to not make it easy to delete.

Luckily, the fix for this is quite simple (compared to some of the other workarounds). When using getNextHighestDepth, it generally returns a depth of 1048575 or greater which is in the ‘reserve’ zones explained by the article above. Which means, if we are going to go be able to delete it, we need it to be within 0 to 1048575 depth.

We can accomplish this using the swapDepths function. Just need to swap the current movie’s depth to something that’s within normal range (and be sure that no movieclip is already present at said depth) and use the removeMovieClip function.

Example:

var mc1:MovieClip = this.createEmptyMovieClip('mc1', 10);
var mc2:MovieClip = this.createEmptyMovieClip('mc2', 1050000);
var mc3:MovieClip = this.createEmptyMovieClip('mc3', this.getNextHighestDepth());
 
mc1.removeMovieClip(); // Works.
mc2.removeMovieClip(); // Doesn't work, over 1048575
mc3.removeMovieClip(); // Same
 
mc2.swapDepths(999999);
mc2.removeMovieClip(); // Now it works
mc3.swapDepths(999998);
mc3.removeMovieClip(); // Same here

Loading