Flash Advisor logo
:: Desktop Shortcut
:: Flash Help
Advice from Experts

+ Reply to Thread
Results 1 to 1 of 1

Thread: Restart game

  1. #1

    Default Restart game

    Hello,

    I'm making a memory game and I want to restart the game when you match all cards. How can I do that?

    I was able to reset the timer but I cant reset the cards.

    I used remove child to remove them from stage and then got them back with addchild again... but no reset... the cards came up again as they left.

    Is there anyone that knows how I should do this?

    Here is my code:
    Code:
    package
    {
        import flash.display.MovieClip;
        import flash.events.MouseEvent;
        import flash.display.Sprite;
        import flash.utils.Timer;
        import flash.events.Event;
        import flash.text.*;
        import flash.events.TimerEvent;
        import Card;
        import Computer;
        import Muis;
        import Radio;
        import Iphone;
        import Beamer;
        import Platenspeler;
        
        public class MemoryGame extends MovieClip
        {
            private var _card:Card;
            private var _computer:Computer;
            private var _muis:Muis;
            private var _radio:Radio;
            private var _iphone:Iphone;
            private var _beamer:Beamer;
            private var _platenspeler:Platenspeler;
            private var _cardX:Number;
            private var _cardY:Number;
            private var _firstCard:*;
            private var _totalMatches:Number;
            private var _currentMatches:Number;
            private var _cards:Array = new Array();
            private var _score:Number;
            
            public function MemoryGame()
            {
                _totalMatches = 6;
                _currentMatches = 0;
                createCards();
                shuffleCards();
            }
            
            private function shuffleCards():void {
                var tmpX:Number;
                var tmpY:Number;
                for each (var items in _cards) {
                    var n:int=Math.floor(Math.random()*_cards.length);
                    tmpX=_cards[n].x;
                    tmpY=_cards[n].y;
                    _cards[n].x=items.x;
                    _cards[n].y=items.y;
                    items.x=tmpX;
                    items.y=tmpY;
                }
            }
            
            private function createCards():void
            {
                _cardX = 45;
                _cardY = 31;
                
                for(var i:Number = 0; i < 2; i++)
                {
                _card = new Card();
                _cards.push(_card);
                addChild(_card);
                _computer = new Computer();
                _card.setType(_computer);
                _card.x = _cardX;
                _card.y = _cardY;
                _cardX += _card.width + 50;
                _card.addEventListener(MouseEvent.CLICK, checkCards);
                }
                
                for(var j:Number = 0; j < 2; j++)
                {
                _card = new Card();
                _cards.push(_card);
                addChild(_card);
                _muis = new Muis();
                _card.setType(_muis);
                _card.x = _cardX;
                _card.y = _cardY;
                _cardX += _card.width + 50;
                _card.addEventListener(MouseEvent.CLICK, checkCards);
                }
                _cardX = 45;
                _cardY += _card.height + 50;
                for(var k:Number = 0; k < 2; k++)
                {
                _card = new Card();
                _cards.push(_card);
                addChild(_card);
                _radio = new Radio();
                _card.setType(_radio);
                _card.x = _cardX;
                _card.y = _cardY;
                _cardX += _card.width + 50;
                _card.addEventListener(MouseEvent.CLICK, checkCards);
                }
                
                for(var l:Number = 0; l < 2; l++)
                {
                _card = new Card();
                _cards.push(_card);
                addChild(_card);
                _iphone = new Iphone();
                _card.setType(_iphone);
                _card.x = _cardX;
                _card.y = _cardY;
                _cardX += _card.width + 50;
                _card.addEventListener(MouseEvent.CLICK, checkCards);
                }
                
                _cardX = 45;
                _cardY += _card.height + 50;
                for(var m:Number = 0; m < 2; m++)
                {
                    _card = new Card();
                    _cards.push(_card);
                    addChild(_card);
                    _beamer = new Beamer();
                    _card.setType(_beamer);
                    _card.x = _cardX;
                    _card.y = _cardY;
                    _cardX += _card.width + 50;
                    _card.addEventListener(MouseEvent.CLICK, checkCards);
                }
                
                for(var n:Number = 0; n < 2; n++)
                {
                    _card = new Card();
                    _cards.push(_card);
                    addChild(_card);
                    _platenspeler = new Platenspeler();
                    _card.setType(_platenspeler);
                    _card.x = _cardX;
                    _card.y = _cardY;
                    _cardX += _card.width + 50;
                    _card.addEventListener(MouseEvent.CLICK, checkCards);
                }
            }
            
            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))
                {
                    
                    _firstCard = undefined;
                    _currentMatches ++;
                    if(_currentMatches >= _totalMatches)
                    {
                        gotoAndStop(2);
                        timer.stop();
                        for (var i:int = 0; i < _cards.length; i++){
                        removeChild(_cards[i]);
                        }
    
                    }
                }
                else
                {
                    
                    _firstCard.gotoAndPlay("flipBack");
                    event.currentTarget.gotoAndPlay("flipBack");
                    _firstCard.addEventListener(MouseEvent.CLICK, checkCards);
                    event.currentTarget.addEventListener(MouseEvent.CLICK, checkCards);
                    _firstCard = undefined;
                }
            }
        }
    }
    Btw I want to reset it with a button click. The timer is reset too by a button. Dont know if its possible with the cards...

    It would be very nice if someone could help me out, I dont know where to begin anymore.
    Attached Files

  2. # ADS
    Join Date
    Always
    Posts
    Many
     

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

     

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
Sponsors
Create Speaking Characters for your website and Flash movies. 15 Day Free Trial