2013-05-07 69 views
0

我有下面的代碼,它工作正常,除了我想要它在庫中播放Explosion movieclip時EnemyShip消失,但我只想它播放一次,然後消失,但不太確定如何做到這一點(我'已經嘗試了幾件事情,它或者使爆炸動畫循環和船舶不會消失,我相信這是因爲將它放入kill函數中,或者我得到其他的ArgumentErrors)。如何讓此代碼正確顯示movieclip?

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(); 
} 

感謝您收到任何幫助。

回答

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(或類似)的函數以避免多餘的代碼(因此您可以在關閉時或在船舶出界時調用此函數)

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 
} 

接下來,更新的殺功能:

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)); 
+0

我嘗試使用'AddFrameScript'解決方案,但它給出了以下錯誤:'ArgumentError:Error#2025:提供的DisplayO bject必須是調用者的孩子。 \t at flash.display :: DisplayObjectContainer/removeChild() \t at MethodInfo-8()' – Liam 2013-05-07 20:26:27

+0

更新了答案。忘了你的爆炸並不是發佈的代碼的上下文的孩子。 – BadFeelingAboutThis 2013-05-07 20:30:17

+0

這三種方法比使用ENTER_FRAME處理程序在爆炸完成時檢查每個單幀有效得多。第一種方法是最乾淨的方法。 – BadFeelingAboutThis 2013-05-07 20:32:40

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); 
    } 
}