0

我有以下代码,它工作正常,除了我希望它在消失时播放Explosion库中的电影剪辑,EnemyShip但我只希望它播放一次然后消失但不太确定该怎么做(我尝试了一些事情,它要么使爆炸动画循环并且船不会消失,我相信这是将它放在kill函数中的结果,或者我得到其他 ArgumentErrors)。

var speed:Number;
var shot = new ShotSound();
var explosion = new Explosion();

this.x = 800;
this.y = Math.random() * 275 + 75;
speed = Math.random()*5 + 9;
addEventListener("enterFrame", enterFrame);
addEventListener(MouseEvent.MOUSE_DOWN, mouseShoot);

function enterFrame(e:Event)
{
   this.x -= speed;
   if(this.x < -100)
{
   removeEventListener("enterFrame", enterFrame);
   stage.removeChild(this);
} 
}

function kill()
{
    stage.addChild(this);
    explosion.x = this.x;
    explosion.y = this.y;
    removeEventListener("enterFrame", enterFrame);
stage.removeChild(this);
    shot.play();
}

function mouseShoot(event:MouseEvent)
{
    kill();
}

感谢您提供的任何帮助。

4

2 回答 2

2

当爆炸到达它的最后一帧时,您需要一些脚本来触发。有几种方法可以做到这一点:

1. 在爆炸动画的最后一帧调度事件。例如。 this.dispatchEvent(new Event(Event.COMPLETE)); 然后监听那个事件:

var explosion = new Explosion();
addChild(explosion);
explosion.addEventListener(Event.COMPLETE, removeExplosion);

function removeExplosion(e:Event):void {
    MovieClip(e.currentTarget).stop();
    MovieClip(e.currentTarget).removeEventListener(Event.COMPLETE, removeExplosion);
    removeChild(MovieClip(e.currentTarget));
}

2. 让爆炸在动画的最后一帧自行消失。例如。if(this.parent) parent.removeChild(this);

3. 如果不能或不想修改爆炸时间线,请使用未记录的 AddFrameScript 命令:

var explosion = new Explosion();
addChild(explosion);
explosion.addFrameScript(explosion.totalFrames-1, function():void {
    explosion.stop();
    if(explosion.parent){
        explosion.parent.removeChild(explosion);
    }
});

这是您设置的提示:

添加一个名为 removeMe (或类似)的函数以避免冗余代码(因此您可以在 kill 或船出界时调用此函数

function removeMe(e:Event = null):void {
    this.removeEventLIstener(Event.ENTER_FRAME,enterFrame);
    if(this.parent){
        this.parent.removeChild(this);
    }
    //any other cleanup that's required
}

接下来,更新的 kill 函数:

function kill(){
    var explosion = new Explosion();
    explosion.addEventListener(Event.COMPLETE, removeMe); //you need to dispatch this event on the last frame of the explosion timeline as shown below
    addChild(explosion); //add it to the ship so it stays with it as the ship moves

    //not sure what shot.play() does and if that belongs here.
}

//on the last frame of your explosion timeline:
stop();
dispatchEvent(new Event(Event.COMPLETE));
于 2013-05-07T19:46:19.050 回答
0

对于初学者,您永远不会添加explosion到显示列表中。

改变

var explosion = new Explosion();
stage.addChild(this);

var explosion = new Explosion();
stage.addChild(explosion);

完成此操作后,您将需要添加一个侦听器explosion以了解它何时完成播放:

explosion.addEventListener(Event.ENTER_FRAME, onExplosionProgress);

function onExplosionProgress(e:Event):void 
{
    if(explosion.currentFrame == explosion.totalFrames)
    {
        // explosion has reached end of its timeline
        explosion.removeEventListener(Event.ENTER_FRAME, onExplosionProgress);
        explosion.stop();
        stage.removeChild(explosion);
    }
}
于 2013-05-07T20:02:34.843 回答