1

我正在创建 Flash“记忆”游戏,想发现 2 张相等的卡片。但是当我选择1时,如果第二张卡错了它只发现半秒,但这还不够。我需要让第二张卡片显示 2-3 秒。如何添加计时器?

以下是部分代码:

        else
        {
            trace("wrong");
            _message = "wrong";
            message_txt.text = _message;
            _firstCard.gotoAndPlay("flipBack");
            event.currentTarget.gotoAndPlay("flipBack");
            _firstCard.addEventListener(MouseEvent.CLICK, checkCards);
            event.currentTarget.addEventListener(MouseEvent.CLICK, checkCards);
            _firstCard = undefined;             
        }

谢谢你的回答。

已更新完整代码

package
{
    import flash.display.MovieClip;
    import Card;
    import Boarder;
    import BlueBoard;
    import flash.events.MouseEvent;
    import RedBoard;
    import Snow;

    public class MemoryGame extends MovieClip
    {
        private var _card:Card;
        private var _boarder:Boarder;
        private var _blueBoard:BlueBoard;
        private var _cardX:Number;
        private var _cardY:Number;
        private var _firstCard:*;
        private var _totalMatches:Number;
        private var _currentMatches:Number;
        private var _redBoard:RedBoard;
        private var _snow:Snow;
        private var _cards:Array;
        public var  _message:String;

        public function MemoryGame()
        {
            _cards = new Array();
            _totalMatches = 4;
            _currentMatches = 0;
            createCards();
        }

        private function createCards():void
        {
            _cardX = 45;
            _cardY = 10;

            for(var i:Number = 0; i < 2; i++)
            {
                _card = new Card();
                addChild(_card);
                _boarder = new Boarder();
                _card.setType(_boarder);
                _card.x = _cardX;
                _card.y = _cardY;
                _cardX += _card.width + 50;
                _card.addEventListener(MouseEvent.CLICK, checkCards);
                _cards.push(_card);
            }

            for(var j:Number = 0; j < 2; j++)
            {
                _card = new Card();
                addChild(_card);
                _blueBoard = new BlueBoard();
                _card.setType(_blueBoard);
                _card.x = _cardX;
                _card.y = _cardY;
                _cardX += _card.width + 50;
                _card.addEventListener(MouseEvent.CLICK, checkCards);
                _cards.push(_card);
            }

            _cardX = 45;
            _cardY = _card.height + 30;

            for(var k:Number = 0; k < 2; k++)
            {
                _card = new Card();
                addChild(_card);
                _redBoard = new RedBoard();
                _card.setType(_redBoard);
                _card.x = _cardX;
                _card.y = _cardY;
                _cardX += _card.width + 50;
                _card.addEventListener(MouseEvent.CLICK, checkCards);
                _cards.push(_card);
            }

            for(var l:Number = 0; l < 2; l++)
            {
                _card = new Card();
                addChild(_card);
                _snow = new Snow();
                _card.setType(_snow);
                _card.x = _cardX;
                _card.y = _cardY;
                _cardX += _card.width + 50;
                _card.addEventListener(MouseEvent.CLICK, checkCards);
                _cards.push(_card);
            }

            randomizeCards(_cards);
        }

        private function checkCards(event:MouseEvent):void
        {
            event.currentTarget.removeEventListener(MouseEvent.CLICK, checkCards);

            if(_firstCard == undefined)
            {
                _firstCard = event.currentTarget;
            }
            else if(String(_firstCard._type) == String(event.currentTarget._type))
            {
                trace("match");
                _message = "match";
                message_txt.text = _message;
                _firstCard = undefined;
                _currentMatches ++;
                if(_currentMatches >= _totalMatches)
                {
                    trace("YOU WIN !!!");
                    _message = "YOU WIN !!!";
                    message_txt.text = _message;
                }
            }
            else
            {
                trace("wrong");
                _message = "wrong";
                message_txt.text = _message;
                _firstCard.gotoAndPlay("flipBack");
                event.currentTarget.gotoAndPlay("flipBack");
                _firstCard.addEventListener(MouseEvent.CLICK, checkCards);
                event.currentTarget.addEventListener(MouseEvent.CLICK, checkCards);
                _firstCard = undefined;             
            }
        }

        private function randomizeCards(cards:Array):void
        {
            var randomCard1:Number;
            var randomCard2:Number;
            var card1X:Number;
            var card1Y:Number;

            for(var i:Number = 0; i < 10; i++)
            {
                randomCard1 = Math.floor(Math.random() * cards.length);
                randomCard2 = Math.floor(Math.random() * cards.length);

                card1X = cards[randomCard1].x;
                card1Y = cards[randomCard1].y;
                cards[randomCard1].x = cards[randomCard2].x;
                cards[randomCard1].y = cards[randomCard2].y
                cards[randomCard2].x = card1X;
                cards[randomCard2].y = card1Y;
            }
        }
    }
}
4

2 回答 2

1

您可以使用http://help.adobe.com/en_US/FlashPlatform/reference/actionscript/3/flash/utils/Timer.html。顺便说一句,请注意 Timer 构造函数的默认第二个参数是 0,这意味着计时器将无限期地运行。请务必为您的案例将其标记为 1。

您需要将“else”块中的代码移动到另一个函数,并在 else 块中设置计时器。

最终代码如下所示:

else
{
    trace("wrong");
    _message = "wrong";
    message_txt.text = _message;

    _secondCard = event.currentTarget;//You need to have this variable defined alongwith _firstCard

    var timer:Timer = new Timer(2000, 1);
    timer.addEventListener(TimerEvent.TIMER_COMPLETE, flipBack);
    timer.start();
}

并且该功能将具有:

protected function flipBack(event:TimerEvent):void
{
    _firstCard.gotoAndPlay("flipBack");
    _sedondCard.gotoAndPlay("flipBack");
    _firstCard.addEventListener(MouseEvent.CLICK, checkCards);
    _secondCard.addEventListener(MouseEvent.CLICK, checkCards);
    _firstCard = _secondCard = undefined;             
}

HTH。

于 2013-04-30T17:24:33.540 回答
0

如果只是等待一定的时间,可以使用 setTimeout (flash.utils)

http://livedocs.adobe.com/flash/9.0_fr/ActionScriptLangRefV3/flash/utils/package.html#setTimeout()

于 2013-04-30T17:37:24.293 回答