2013-04-08 81 views
0

它們並不是真正的敵人,它們只是你射擊的氣球。但是,當他們添加addChild時,他們都沒有在播放他們的「死亡」動畫,當我點擊(拍攝)他們。這是我的代碼。請原諒,如果它顯得雜亂無章,我剛開始沒有OOP經驗的ActionScript。殺死遊戲敵人ActionScript 3.0

一切正常,除了點擊兒童似乎根本沒有註冊添加到舞臺上的對象。我沒有外部類,所有實例名都是正確的。我把聯繫中的氣球稱爲「受害者」。

import flash.display.MovieClip; 
import flash.utils.Timer; 
import flash.events.MouseEvent; 


Mouse.hide(); 
cursor_mc.startDrag(true); 


stage.addEventListener(MouseEvent.MOUSE_DOWN, onClick); 
shotHandler.addEventListener(MouseEvent.MOUSE_DOWN, boxShot); 

function boxShot(evt:MouseEvent):void 
{ 
    enemyBox.gotoAndStop(2); 
} 




    function onClick(event:MouseEvent):void 

    { 
     cursor_mc.play(); 
     var myBullet:MovieClip = new black_mc(); 
     myBullet.x = mouseX; myBullet.y = mouseY; 
     stage.addChildAt(myBullet , 0); 
    } 


    var myTimer:Timer = new Timer(1200, 300); 
    myTimer.addEventListener(TimerEvent.TIMER, createEnemies); 
    myTimer.start(); 

    function createEnemies(e:Event):void 
    { 


     var circle:MovieClip = new victim(); 
     circle.x = Math.random() * stage.stageWidth; 
     circle.y = Math.random() * stage.stageHeight; 
     addChildAt(circle , 2); 


    } 

回答

0

我查看了你的代碼,並且在「受害者」對象上看不到任何MouseListeners的添加。無論如何,如果你點擊它需要用對象做點什麼,你可以寫下類似的東西:

function createEnemies(e:Event):void //your enemy creation function 
{ 


    var circle:MovieClip = new victim(); 

    circle.addEventListener(MouseEvent.CLICK, onObjClick); // here is your click listener 

    circle.x = Math.random() * stage.stageWidth; 
    circle.y = Math.random() * stage.stageHeight; 
    addChildAt(circle , 2); 


} 

private function onObjClick(event:MouseEvent):void { 
    var target:MovieClip = event.currentTarget as victim; 
    target.gotoAndPlay("destroyAnim"); 
    //target.goToDeath(); uncomment this and comment previous line if you already have a destroy function into your "victim" class. 
}