2013-04-24 87 views
0

我有一些困難搞清楚如何讓我的動畫工作,以便當一個對象(影片剪輯)被點擊時,一個函數被調用,並說該函數從屏幕上移除該對象,並顯示一個爆炸。代碼如下(從我EnemyShip.as文件):點擊鼠標,調用函數?

package 
{ 
import flash.display.MovieClip; 
import flash.events.Event; 
import flash.utils.Timer; 
import flash.events.MouseEvent; 

public class EnemyShipMed extends MovieClip 
{ 
    var speed:Number; 

    function EnemyShipMed() 
    { 
     this.x = 800; 
     this.y = Math.random() * 275 + 75; 
     speed = Math.random()*5 + 6; 
     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() 
     { 
      var explosion = new Explosion(); 
      stage.addChild(explosion); 
      explosion.x = this.x; 
      explosion.y = this.y; 
      removeEventListener("enterFrame", enterFrame); 
      stage.removeChild(this); 
     } 

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

我更換了一個十字鼠標指針(影片剪輯名爲crosshair_mc)。這是我的Main.as文件中的內容:

package { 
import flash.display.MovieClip; 
import flash.events.Event; 
import flash.events.MouseEvent; 
import flash.utils.Timer; 
import flash.ui.Mouse; 
import flash.media.Sound; 
import flash.media.SoundChannel; 

public class Main extends MovieClip { 

public var crosshair:crosshair_mc; 
var enemyShipTimer:Timer; 
var enemyShipTimerMed:Timer; 
var enemyShipTimerSmall:Timer; 

public function Main() 
{ 
    enemyShipTimer = new Timer(2000); 
    enemyShipTimer.addEventListener("timer", sendEnemy); 
    enemyShipTimer.start(); 

    enemyShipTimerMed = new Timer(2500); 
    enemyShipTimerMed.addEventListener("timer", sendEnemyMed); 
    enemyShipTimerMed.start(); 

    enemyShipTimerSmall = new Timer(2750); 
    enemyShipTimerSmall.addEventListener("timer", sendEnemySmall); 
    enemyShipTimerSmall.start(); 

    //This creates a new instance of the cursor movie clip and adds it onto 
    //the stage using the addChild method. 
    crosshair = new crosshair_mc(); 
    addChild(crosshair); 

    //Hides the default cursor on the stage so it will not be shown. 
    Mouse.hide(); 

    //Adds an event listener onto the stage with the enter frame event which 
    //repeatedly executes the moveCursor function. 
    stage.addEventListener(Event.ENTER_FRAME, moveCursor); 
} 

function sendEnemy(e:Event) 
{ 
    var enemy = new EnemyShip(); 
    stage.addChild(enemy); 
    stage.addChild(crosshair);//brings crosshair to topmost level on stage 
} 

function sendEnemyMed(e:Event) 
{ 
    var enemymed = new EnemyShipMed(); 
    stage.addChild(enemymed); 
    stage.addChild(crosshair);//brings crosshair to topmost level on stage 
} 

function sendEnemySmall(e:Event) 
{ 
    var enemysmall = new EnemyShipSmall(); 
    stage.addChild(enemysmall); 
    stage.addChild(crosshair);//brings crosshair to topmost level on stage 
} 
//This function set the x & y positions of the custom cursor to the x & y positions 
//of the default cursor. 
function moveCursor(event:Event) 
{ 
    crosshair.x=mouseX; 
    crosshair.y=mouseY; 
    } 
    } 
} 

謝謝。

+1

什麼問題?你可以測試你是否輸入mouseShoot eventListener? – Kodiak 2013-04-24 15:57:24

+0

基本上什麼都沒有發生。我點擊對象,它沒有做任何事情。 – David 2013-04-24 16:18:14

+0

運行2'trace()'測試:在'mouseShoot()'中測試一個,在'kill()'中測試一個。看看他們是否開火。 – David 2013-04-24 16:19:31

回答

1

我的猜測是你的十字線會捕獲所有的點擊事件,因爲它總是在鼠標下。設置

crosshair_mc.mouseEnabled = crosshair_mc.mouseChildren = false; 

使「透明」的鼠標事件。

+0

在我的主文章中增加了更多信息 – David 2013-04-24 16:32:42

+0

所以你試過我的提示,它沒有工作? – Kodiak 2013-04-24 16:33:41

+0

好吧,我試着把它放在Main.as文件中,並且得到一個錯誤,說'通過帶有靜態類型Class的引用訪問可能未定義的屬性MouseChildren。 'mouseEnabled'相同 – David 2013-04-24 16:35:37