Mar 11
2011

A bug with for…in vs for

This is possibly the weirdest bug I’ve ever seen. My flash program was working fine from within the debugger and from Adobe’s swf player built into Flash CS5. However, when I put the swf into a web page, I got errors from things not being deleted.

How I make sure everything is deleted

Basically, I’ve borrowed what I’m sure is a common method of keeping track of objects from the Kongregate tutorials. Whenever I create a sprite or a text object or whatever, I push the object to a list that’s a class variable. Then when the objects need to be destroyed, it’s easy to go through the list and find them all. One example of this was in my side-scrolling space ship shooter game. Every time the ship fired a bullet, the bullet sprite would get added to the screen and the bullet would be added to a list. When the main ship got destroyed, then a function would be called to go through the list of bullets and remove them from the screen, turn off their event listeners, etc…

The code that caused the weird bug (only on some platforms):

[as3]
function destroy() {
for (var i in components){
components[i].destroy();
delete components[i];
}
removeEventListener("enterFrame", enterFrame);
if (this.parent && this.parent == stage) stage.removeChild(this);
}
[/as3]

The code that works everywhere:

[as3]
function destroy() {
for (var i = 0; i < components.length; i++){
components[i].destroy();
delete components[i];
}
removeEventListener("enterFrame", enterFrame);
if (this.parent && this.parent == stage) stage.removeChild(this);
}
[/as3]

Huh? How the heck is one different from the other?

This entry was tagged , , .
Bookmark the permalink.

Leave a Reply

Your email address will not be published. Required fields are marked *