Question

I'm making a game using Flash Pro cc.(ActionScript3) But I wonder what happens to aTile, which is dynamically declared MovieClip variable through a loop. And each aTile gets the 2 EventListener's.

for (var i:Number=0; I < pVector.length;i++){

var aTile:ATile=new ATile();

aTile.x=pVector[i].x;

aTile.y=pVector[i].y;

aTile.gotoAndStop(Math.ceil(Math.random()*Color));

nVector.push(aTile);



Spr.addChild(aTile);


aTile.addEventListener(MouseEvent.CLICK,Clicked,false,0,true);

aTile.addEventListener(Event.COMPLETE, stop,false,0,true);

}

// the current function ends here. what happens to aTile now ?? Is it going to be garbage collected? By the way, this piece of code runs whenever a player starts a new level of my game. And I don't make use of the aTile variable in other functions. I use only the nVector variable. And does declaring a dynamic variable in a loop mean a multiple of them are created? For example, if I loop the piece of code above 5 times, does it mean 5 aTile variables are created? Or each time you declare var aTile:ATile=new ATile(); again, does it replace the 'old' aTile with the 'new' aTile and therefore only 1 aTile exists after the loop ????

Was it helpful?

Solution

ActionScript is a derivative of JavaScript. In both languages, all variables declaration are hoisted to the top of the containing function and are accessible throughout the function, regardless of which block scope they were declared in.

Memory is reclaimed when all the references to the object are no longer accessible to the program. Since you're adding aTile to nVector and to the Sprite, it will only be reclaimed after the function returns, nVector goes out of scope, and Spr goes out of scope and is removed from the stage.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top