设置倒计时并在游戏达到0时停止游戏

时间:2015-10-14 05:01:29

标签: actionscript-3 flash timer

我在AS3中有一个简单的游戏,我正在尝试倒计时,所以当倒计时到0时游戏结束。

我在测试此代码时没有收到任何错误,但我也没有得到倒计时时钟。在项目中,我创建了一个文本框,并使用嵌入字体使其动态化。

    package {
     import flash.display.*;
     import flash.events.*;
     import flash.text.*;
     import flash.utils.getTimer;
     import flash.utils.Timer;
     import flash.media.Sound;
     import flash.media.SoundChannel;



    public class MatchingGameObject10 extends MovieClip {
        // game constants
        private static const boardWidth:uint = 2;
        private static const boardHeight:uint = 2;
        private static const cardHorizontalSpacing:Number = 52;
        private static const cardVerticalSpacing:Number = 52;
        private static const boardOffsetX:Number = 145;
        private static const boardOffsetY:Number = 70;
        private static const pointsForMatch:int = 50;
        private static const pointsForMiss:int = -5;

        // variables
        private var firstCard:Card10;
        private var secondCard:Card10;
        private var cardsLeft:uint;
        private var gameScore:int;
        private var gameStartTime:uint;
        private var clockTimer:Timer;

        // text fields
        private var gameScoreField:TextField;
        private var clock:TextField;

        // timer to return cards to face-down
        private var flipBackTimer:Timer;

        // set up sounds
        var theFirstCardSound:FirstCardSound = new FirstCardSound();
        var theMissSound:MissSound = new MissSound();
        var theMatchSound:MatchSound = new MatchSound();

        // initialization function
        public function MatchingGameObject10():void {
            // make a list of card numbers
            var cardlist:Array = new Array();
            for(var i:uint=0;i<boardWidth*boardHeight/2;i++) {
                cardlist.push(i);
                cardlist.push(i);
            }

            // create all the cards, position them, and assign a randomcard face to each
            cardsLeft = 0;
            for(var x:uint=0;x<boardWidth;x++) { // horizontal
                for(var y:uint=0;y<boardHeight;y++) { // vertical
                    var c:Card10 = new Card10(); // copy the movie clip
                    c.stop(); // stop on first frame
                    c.x = x*cardHorizontalSpacing+boardOffsetX; // set position
                    c.y = y*cardVerticalSpacing+boardOffsetY;
                    var r:uint = Math.floor(Math.random()*cardlist.length); // get a random face
                    c.cardface = cardlist[r]; // assign face to card
                    cardlist.splice(r,1); // remove face from list
                    c.addEventListener(MouseEvent.CLICK,clickCard); // have it listen for clicks
                    c.buttonMode = true;
                    addChild(c); // show the card
                    cardsLeft++;
                }
            }

            // set up the score
            gameScoreField = new TextField();
            addChild(gameScoreField);
            gameScore = 0;
            showGameScore();
         }   
            // set up the clock

        public function CountdownClock() {
            startClock();
        }
        public function startClock() {
            clockTimer = new Timer(1000,10);
            clockTimer.addEventListener(TimerEvent.TIMER, clockTick);
            clockTimer.addEventListener(TimerEvent.TIMER_COMPLETE, clockEnd);
            clockTimer.start();
            showClock();
        }

        public function clockTick(event:TimerEvent) {
            showClock();
        }

        public function showClock() {
            clock.text = String(clockTimer.repeatCount - clockTimer.currentCount);
        }


        public function clockEnd(event:TimerEvent) {
            clock.text = "!";
        }

        // player clicked on a card
        public function clickCard(event:MouseEvent) {
            var thisCard:Card10 = (event.target as Card10); // what card?

            if (firstCard == null) { // first card in a pair
                firstCard = thisCard; // note it
                thisCard.startFlip(thisCard.cardface+2);
                playSound(theFirstCardSound);

            } else if (firstCard == thisCard) { // clicked first card again
                firstCard.startFlip(1);
                firstCard = null;
                playSound(theMissSound);

            } else if (secondCard == null) { // second card in a pair
                secondCard = thisCard; // note it
                thisCard.startFlip(thisCard.cardface+2);

                // compare two cards
                if (firstCard.cardface == secondCard.cardface) {
                    // remove a match
                    removeChild(firstCard);
                    removeChild(secondCard);
                    // reset selection
                    firstCard = null;
                    secondCard = null;
                    // add points
                    gameScore += pointsForMatch;
                    showGameScore();
                    playSound(theMatchSound);
                    // check for game over
                    cardsLeft -= 2; // 2 less cards
                    if (cardsLeft == 0) {
                        MovieClip(root).gameScore = gameScore;
                        MovieClip(root).clockTimer = clockTimer;
                        MovieClip(root).gotoAndStop("gameover");
                    }
                } else {
                    gameScore += pointsForMiss;
                    showGameScore();
                    playSound(theMissSound);
                    flipBackTimer = new Timer(2000,1);
                    flipBackTimer.addEventListener(TimerEvent.TIMER_COMPLETE,returnCards);
                    flipBackTimer.start();
                }

            } else { // starting to pick another pair
                returnCards(null);
                playSound(theFirstCardSound);
                // select first card in next pair
                firstCard = thisCard;
                firstCard.startFlip(thisCard.cardface+2);
            }
        }

        // return cards to face-down
        public function returnCards(event:TimerEvent) {
            if (firstCard != null) firstCard.startFlip(1);
            if (secondCard != null) secondCard.startFlip(1);
            firstCard = null;
            secondCard = null;
            flipBackTimer.removeEventListener(TimerEvent.TIMER_COMPLETE,returnCards);
        }

        public function showGameScore() {
            gameScoreField.text = "Score: "+String(gameScore);
        }

        public function playSound(soundObject:Object) {
            var channel:SoundChannel = soundObject.play();
        }


}
}

1 个答案:

答案 0 :(得分:0)

在您的问题中,您说的是您已添加到舞台上的文字字段,但在这种情况下,如果您的文字字段为“private var clock:TextField;,则您当前的代码应该会出错”名为clock,否则(您的文字字段名称不是clock)您可以在showClock()函数中使用该名称,不要忘记致电CountdownClock()计时器:

public function MatchingGameObject10(): void 
{   
    // ...

    CountdownClock();
}

public function showClock(): void
{
    your_clock_textfield.text = String(clockTimer.repeatCount - clockTimer.currentCount);
}

您也可以像使用clock一样创建gameScoreField文本字段,在这种情况下,您可以删除已经插入阶段的文本字段:

public function MatchingGameObject10(): void 
{   
    // ...

    clock = new TextField();
    addChild(clock);

    CountdownClock();
}

另外,不要忘记设置文本字段的位置,因为默认情况下它们将插入相同的位置(0,0)。

要动态嵌入字体(使用ActionScript),请查看my answer of this question

希望可以提供帮助。