我有以下代码并且它工作正常,除了我希望它在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();
}
感谢您提供的任何帮助。
答案 0 :(得分:2)
当Explosion到达最后一帧时,你需要一些脚本来触发。有几种方法可以做到这一点:
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));
答案 1 :(得分: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);
}
}