Actionscript 3重置游戏?

时间:2012-03-29 04:07:21

标签: actionscript-3

我在这里有一个记忆游戏,当游戏结束或计时器用完时,用户将被带到游戏页面,其中包含一个重置按钮。

我的问题是当我点击重置按钮时,它会将我带回主游戏页面,它会做各种奇怪的事情。

另外,当我通过页面进入游戏时,我的计时器出错了。

这是我的代码:

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

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 seconds:Number;
var minutes:Number;


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);

    // LEVEL FUNCTION
    easyBtn.addEventListener(MouseEvent.CLICK, easyButtonClicked);
    medBtn.addEventListener(MouseEvent.CLICK, medButtonClicked);
    hardBtn.addEventListener(MouseEvent.CLICK, hardButtonClicked);

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

    //Level button events
    function easyButtonClicked(e:MouseEvent):void{
        removeChild(levelText);
        trace("easy button clicked!");
        seconds = 0;
        minutes = 1;
        txtTime.text = "1:00";
        levelTimer.start();
        setupTiles();
    }

    function medButtonClicked(e:MouseEvent):void{
        removeChild(levelText);
        trace("medium button clicked!");
        seconds = 30;
        minutes = 0;
        txtTime.text = "0:30";
        levelTimer.start();
        setupTiles();
    }

    function hardButtonClicked(e:MouseEvent):void{
        removeChild(levelText);
        trace("hard button clicked!");
        seconds = 15;
        minutes = 0;
        txtTime.text = "0:15";
        levelTimer.start();
        setupTiles();
    }


    //Timer handlers
    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();

    }



    //Tiles set up
    function setupTiles(){
    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-0.5) * 150;
            tile.y = (y-0.5) * 200;
            tile.addEventListener(MouseEvent.CLICK,tileClicked);
            addChild(tile);
            cardCount = cardCount + 1
        }
    }
}


}




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();
        }
    }

    if (seconds == 0){
        pauseTimer.stop();
        this.gotoAndStop(3);


    }
}


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;
        pauseTimer.stop();          
        }
            }
}   
}

这是重置按钮的代码:

button_3.addEventListener(MouseEvent.CLICK, resetGame);

function resetGame(event:MouseEvent):void
{
score = 0;
seconds = 0;
pauseTimer.stop();

this.gotoAndStop(1);

}

1 个答案:

答案 0 :(得分:0)

请确保在重置方法之前移除所有事件侦听器。此文件在此之后。

button_3.addEventListener(MouseEvent.CLICK, resetGame);

function resetGame(event:MouseEvent):void
{
score = 0;
seconds = 0;
pauseTimer.stop();

levelTimer.removeEventListener(TimerEvent.TIMER_COMPLETE, timerCompleteHandler);
levelTimer.removeEventListener(TimerEvent.TIMER, timerHandler);

this.gotoAndStop(1);

}

这只是一个例子。你应该删除所有的事件监听器,因为this.gotoAndStop(1);只会添加更多的侦听器,而不会删除前一帧中的侦听器。这就是为什么一切都变得混乱的原因。