所以我创造了一个有趣的小项目昨晚涉及创建大量的小圆圈(星)。所以为了代表这颗明星,我创建了一个明星班。这是最相关的方法影片剪辑去除AS3
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);
}
}
为了给你什么它做了总结,基本上就初始化它增加了对自身的监听器,基本上是空值每个实例varialbles。当animate()被调用时,它会向本身添加一个侦听器,并将其动画到某个方向。这个听众还检查它的位置是否已经在舞台之外,并且在它已经到达时停止它的移动。另外,它会派发一个事件,以便父母知道何时删除它。
因此,在“主”类,我有
this.addEventListener("RemoveStar", removeStar);
和
private function removeStar(e:Event):void
{
starCount--;
this.removeChild(Star(e.target));
}
我还有一个听众,基本上打印出数儿,它有每次,但我不是要去把代码放在这里。我遇到的问题是......看起来像删除星星的听众不能“一直”工作。当创建1000个明星在启动时,没有别的,儿童人数下降在开始的时候,它被卡在一定数量这使我觉得有一些影片剪辑不会移除。有人知道这里发生了什么吗?
你能向我们展示完整的代码?有不同的东西丢失:所有的类属性,以及一些类的方法,如ISNULL()或onRemoval() – pkyeck
哪里'onRemoval()'函数,想必应该去掉所有的内部引用? – shanethehat