2012-12-25 83 views
1

即時通訊工作在fps遊戲。遊戲包含多個敵人。但即時通訊與敵人相同的動畫片段。該movieclip發送火災事件,並減少玩家的生活。但我需要知道哪個movieclip正在調度事件。我隨機添加敵人,我需要知道剛剛開火的敵人的敵人位置。下面有一些代碼,可以幫助...as3如何跟蹤哪個movieclip正在調度事件

dispatchEvent(new Event('Enemy_Fired')); // this event is dispatching from enemy movieclip 

this.addEventListener('Enemy_Fired',ReduceLife); 


    public function ReduceLife(e:Event){ 
     life--; 
     var currentLife:int = life * lifeRatio; 
     if(life<1){ 
      Game_Over(); 
      //game_completed(); 
     } else { 
      sview.playerhit.gotoAndPlay(2); 
      sview.lifebar.gotoAndStop(100 - currentLife); 
      sview.health.text = String(currentLife); 
     } 
//Here i need to know this event dispatched from which enemy 
    } 

在此先感謝

回答

4

你可以得到通過使用調度該事件的對象的引用:

e.target 

好像父正在調度該事件,正如在這行代碼中看到的那樣。

dispatchEvent(new Event('Enemy_Fired')); // this event is dispatching from enemy movieclip

因爲dispatchEvent相同this.dispatchEvent,這意味着你的根類調度事件。

您需要將其更改爲這個 yourEnemyMovieClip.dispatchEvent(new Event('ENEMY_FIRED',true,false);

請注意,我把你的事件的氣泡財產上的真實,取消虛假。氣泡意味着您的活動將在顯示鏈上起泡。這很重要,因爲您正在根類中偵聽事件,這比分派事件的影片剪輯高一級。

請參閱事件類,這裏的構造:在你的事件偵聽 http://help.adobe.com/en_US/FlashPlatform/reference/actionscript/3/flash/events/Event.html

添加以下

e.stopImmediatePropagation(); 

這將向上冒泡任何更高的displaychain停止的情況下,節能性能在應用。

+1

我之前檢查過,它顯示我的根類,我在哪裏捕捉事件,我需要從哪裏調度事件的動畫片... –

+0

啊,我明白了,我更新了我的答案! – user1574041