flash - MovieClip removal in AS3 -


so creating small project fun last night involves creating lot of little circles (star). represent star, created star class. here 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 nan. failed animate");             }             if(animated){                 throw new error("star animated");             }             if(this.parent == null){                 throw new error("star has not been added 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 summary on does, on initialization adds listener nulls every instance varialbles. when animate() called, adds listener animates direction. listener checks whether location outside stage , stops movement when is. additionall, dispatch event parent know when remove it.

so in "main" class, have

this.addeventlistener("removestar", removestar); 

and

private function removestar(e:event):void         {             starcount--;             this.removechild(star(e.target));         } 

i have listener prints out number children has everytime, not gonna put code here. problem have that... looks listener removes star not work "all time". when create 1000 stars during start , nothing else, number of children goes down @ beginning , gets stuck @ number leads me think there movie clips not removed. know happening here?

check whether removing event listeners, removestar instance... don't see how attach removestar listener "main" yet remove star function correctly references e.target property star...

basically, need "kill" references object "released" sometime in future when gc it's pass.

the reason though not "removing" stars right , bottom edge checked in "remove check" code... stars go left or up... add additional conditionals left , , might fix itself... (this.x < 0 || this.y < 0)


Comments

Popular posts from this blog

c# - How to set Z index when using WPF DrawingContext? -

razor - Is this a bug in WebMatrix PageData? -

visual c++ - Using relative values in array sorting ( asm ) -