user1816133
user1816133

Reputation:

Action Script 3. How to add timer?

I'm creating Flash "memory" game, Idea to discover 2 equal cards. But when I choose 1, if second card is wrong It discover only for half second, but this is not enough. I need to make that second card will be shown for 2-3 seconds. How can I add timer?

Here is part of code:

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

Thank you for answers.

UPDATED WITH FULL CODE

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

Upvotes: 1

Views: 254

Answers (2)

catholicon
catholicon

Reputation: 1165

You can use http://help.adobe.com/en_US/FlashPlatform/reference/actionscript/3/flash/utils/Timer.html. BTW, do note that Timer constructor's default 2nd parameter is 0 implying that the timer would run indefinitely. Be sure to mark that as 1 for your case.

You need to move code in your 'else' block to another function and setup the timer instead in the else block.

The final code would look something like this:

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

and the function would have:

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.

Upvotes: 1

RafH
RafH

Reputation: 4544

If it's only to wait a certain amount of time, you can use setTimeout (flash.utils)

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

Upvotes: 0

Related Questions