Hey everyone so I was wondering if recreating a variable movie clip inside a timer object every time it's called could decrease performance or cause a MEMORY LEAK. This is how I have it set up right now:
private function addSwimPowerUp(e:TimerEvent):void
{
var newSwimPower:mcSwimPower = new mcSwimPower();
stage.addChild(newSwimPower);
aSwimPowerUpArray.push(newSwimPower);
}
I do that with all my timer objects in my game. Should i just declare the var newSwimPower
a private variable?
Creating a new instance of something and then not doing anything with it will result in its eventual garbage collection. That is, if your code was this:
private function addSwimPowerUp(e:TimerEvent):void
{
var newSwimPower:mcSwimPower = new mcSwimPower();
}
Then all of those new MovieClips will get cleant up.
With that said, you're adding those objects to the display tree as well asn an array. If you do not remove the object from both of those things, it will not get garbage collected and that will be a memory leak.
Creating a class level variable for newSwimPower
and assigning to that instead won't make any difference, other than that if you don't garbage collect the class referencing it then the last instance you created will still be in memory.