So I was creating a small project for fun last night that involves creating a lot of little circles (star). So to represent this star, I created a star class. Here it is with the most relevant methods
public class Star extends MovieClip
{
public function Star(r:Number)
{
starRadius = r;
this.animated = false;
this.graphics.beginFill(0xFFFFFF);
this.graphics.drawCircle(0,0,starRadius);
this.graphics.endFill();
this.addEventListener(Event.REMOVED, onRemoval);
}
public function animate():void
{
if( isNull(angle)|| isNull(speed)){
throw new Error("Angle or speed is NaN. Failed to animate");
}
if(animated){
throw new Error("Star already animated");
}
if(this.parent == null){
throw new Error("Star has not been added to stage yet");
}
this.addEventListener(Event.ENTER_FRAME, doAnimate, false, 0);
animated = true;
}
private function doAnimate(e:Event):void{
if(isNull(cachedDirectionalSpeedX) || isNull(cachedDirectionalSpeedY)){
cachedDirectionalSpeedY = -speed * Math.sin(angle);
cachedDirectionalSpeedX = speed * Math.cos(angle);
}
this.x += cachedDirectionalSpeedX;
this.y += cachedDirectionalSpeedY;
if(this.x > this.parent.stage.stageWidth || this.y > this.parent.stage.stageHeight){
this.dispatchEvent(new Event("RemoveStar",true));
this.removeEventListener(Event.ENTER_FRAME, doAnimate);
}
}
To give you a summary on what it does, basically on initialization it adds to itself a listener which basically just nulls every instance varialbles. When animate() is called, it adds a listener to itself that animates to a certain direction. This listener also checks whether its location is already outside the stage and stops its movement when it already is. Additionall, it will dispatch an event so that the parent will know when to remove it.
So in the “main” class, I have
this.addEventListener("RemoveStar", removeStar);
and
private function removeStar(e:Event):void
{
starCount--;
this.removeChild(Star(e.target));
}
I have another listener that basically prints out the number children that it has everytime, but I am not gonna put the code here. THe problem that I have is that… it looks like the listener that removes the star does not work “all the time”. When create 1000 stars during start up and nothing else, the number of children goes down at the very beginning and it gets stuck at a certain number which leads me to think that there are some movie clips do not get removed. Does anyone know what is happening here?
Check whether you are removing all event listeners, removeStar for instance… I don’t see how you attach the removeStar listener to “Main” yet the remove star function correctly references the e.target property to be a star…
Basically, you need to “kill” all references to the object for it to be “released” sometime in the future when the GC does it’s pass.
The reason though you are not “removing” all the stars is probably that only right and bottom edge are checked in your “remove check” code… probably some stars go left or up… add additional conditionals for left and up and it might fix itself… (this.x < 0 || this.y < 0)