我在这里有一个游戏,我想在我的按钮上添加事件处理程序来改变时间。我知道如何做到这一切,我只是麻烦地将事件监听器添加到我的.as文件中的按钮。我有一个名为“btnEasy”的按钮,链接为“btnEasy”。
我收到此错误:“1061:通过带有静态类型Class的引用调用可能未定义的方法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 :(得分:0)
如果您有一个名为btnEasy
的库符号,那么这是它的类名。也就是说,您尝试在类而不是类的实例上使用addEventListener()
- 类似于通过.round()
类本身调用.cos()
和Math
的方式而不是需要创建Math
。
您需要创建btnEasy
的实例并将事件监听器添加到该实例:
var btn:btnEasy = new btnEasy();
btn.addEventListener( ... );
您正在做的与此类似:
MovieClip.addEventListener( ... );