2012-03-19 234 views
0

我在這裏有一個遊戲,我想添加事件處理程序到我的按鈕來改變時間。我知道如何做到這一點,我只是在爲我的.as文件中的按鈕添加事件監聽器而煩惱。我有一個名爲「btnEasy」的按鈕,連接也是「btnEasy」。無法在actionscript 3中添加鼠標事件處理程序

我收到此錯誤:「1061:通過與靜態類型類的引用調用可能未定義的方法的addEventListener」

這裏是我的代碼:

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

public class MemoryGame extends MovieClip{ 

    private var firstTile:cards; 
    private var secondTile:cards; 
    private var pauseTimer:Timer; 
    private var score:int; 
    private var cardCount:int; 

    var numberDeck:Array = new Array(1,1,2,2,3,3,4,4,5,5,6,6); 
    public function MemoryGame(){ 


     //TIMER FUNCTION 
     var levelTimer:Timer = new Timer(1000, 180); 
     levelTimer.addEventListener(TimerEvent.TIMER_COMPLETE, timerCompleteHandler); 
     levelTimer.addEventListener(TimerEvent.TIMER, timerHandler); 
     btnEasy.addEventListener(MouseEvent.CLICK, easyButtonClicked); 
     var seconds:Number = 0; 
     var minutes:Number = 1; 
     txtTime.text = "1:00"; 
     levelTimer.start(); 

     function easyButtonClicked(e:MouseEvent):void{ 
      trace("easy button clicked!"); 
     } 

     function timerHandler(e:TimerEvent):void { 

     if (seconds > 00) { 

     seconds -=1; 

     } else { 
      if (minutes > 0) {minutes -=1;seconds = 59;} 
     } 

     txtTime.text = minutes+":"+(seconds >= 10 ? seconds : "0"+seconds); 

      } 

     function timerCompleteHandler(e:TimerEvent):void { 

      e.target.reset(); 
      e.target.stop(); 
      txtTime.text="TIMES UP!"; 
     } 





     score = 0; 
     txtScore.text=""+score; 

     for(x=1; x<=4; x++) { 
      for (y=1; y<=3; y++){ 
       var randomCard = Math.floor(Math.random()*numberDeck.length); 
       var tile:cards = new cards(); 
       tile.card = numberDeck[randomCard]; 
       numberDeck.splice(randomCard,1); 
       tile.gotoAndStop(9); 
       tile.x = (x-1) * 150; 
       tile.y = (y-1) * 200; 
       tile.addEventListener(MouseEvent.CLICK,tileClicked); 
       addChild(tile); 
       cardCount = cardCount + 1 
      } 
     } 
     trace("Cardcount: "+cardCount); 
    } 

    public function tileClicked(event:MouseEvent) { 
     var clicked:cards = (event.currentTarget as cards); 
     if (firstTile == null){ 
      firstTile = clicked; 
      firstTile.gotoAndStop(clicked.card); 
     } 
     else if (secondTile == null && firstTile != clicked){ 
      secondTile = clicked; 
      secondTile.gotoAndStop(clicked.card); 
      if (firstTile.card == secondTile.card){ 
       pauseTimer = new Timer(1000, 1); 
       pauseTimer.addEventListener(TimerEvent.TIMER_COMPLETE,removeCards); 
       pauseTimer.start(); 

      } 
      else { 
       pauseTimer = new Timer(1000, 1); 
       pauseTimer.addEventListener(TimerEvent.TIMER_COMPLETE,resetCards); 
       pauseTimer.start(); 
      } 
     } 

    } 

    public function resetCards(event:TimerEvent) { 

     firstTile.gotoAndStop(9); 
     secondTile.gotoAndStop(9); 
     firstTile = null; 
     secondTile = null; 
     pauseTimer.removeEventListener(TimerEvent.TIMER_COMPLETE,resetCards); 
     score = score - 2; 
     txtScore.text=""+score; 
    } 

    public function removeCards(event:TimerEvent){ 

     removeChild(firstTile); 
     removeChild(secondTile); 
     firstTile = null; 
     secondTile = null; 
     pauseTimer.removeEventListener(TimerEvent.TIMER_COMPLETE,removeCards); 
     score = score + 10; 
     txtScore.text=""+score; 
     cardCount = cardCount - 2; 
     trace("Cardcount: " + cardCount); 

     if (cardCount == 0){ 
      this.gotoAndStop(2); 
      txtFinalScore.text=" "+score; 
     } 
       } 

} 
} 

的錯誤是在「 btnEasy.addEventListener(MouseEvent.CLICK,easyButtonClicked);「

任何幫助都會很棒!謝謝!

+0

在屬性btnEasy通過庫訪問,你看到「導出第一幀」?此外,是否在btnEasy實例出現在時間軸之前或之後執行此代碼? – ariestav 2012-03-19 01:05:06

回答

0

如果您有一個名爲btnEasy的圖書館符號,這是它的類名。也就是說,您試圖在類上使用addEventListener()而不是類的實例 - 類似於.round().cos()通過Math類本身調用,而不需要創建Math的實例。

你需要創建的btnEasy的實例和事件偵聽器添加到:

var btn:btnEasy = new btnEasy(); 
btn.addEventListener(...); 

你在做什麼類似於這樣:

MovieClip.addEventListener(...); 
+0

啊是的!完美工作!我早該知道。 但現在,它不打印「輕鬆按鈕點擊!」輸出。 :/ – DommyCastles 2012-03-19 01:17:21

+0

如果您將一個'btnEasy'的實例添加到時間線中,並且您試圖將其作爲目標,那麼您只需爲該按鈕指定一個不同的名稱即可。刪除'var btn:btnEasy = new btnEasy()'並將屬性面板中的實例名稱更改爲'btn'。 – Marty 2012-03-19 01:19:24

+0

很棒!非常感謝你Marty! – DommyCastles 2012-03-19 01:24:14