我在這裏有一個遊戲,我想添加事件處理程序到我的按鈕來改變時間。我知道如何做到這一點,我只是在爲我的.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);「
任何幫助都會很棒!謝謝!
在屬性btnEasy通過庫訪問,你看到「導出第一幀」?此外,是否在btnEasy實例出現在時間軸之前或之後執行此代碼? – ariestav 2012-03-19 01:05:06