-
Restart entire game
Hello,
I'm making a game and I want to restart the entire game after its finished.
The movieclips are on stage with addChild function. So I thought maybe I can make a if statement and use removeChild to remove the movieclips. Now only one movieclip is removed and all the other are still on.
Is there another way to restart the whole game with a button or something?
Here is the code:
Actionscript 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(); } } else { _firstCard.gotoAndPlay("flipBack"); event.currentTarget.gotoAndPlay("flipBack"); _firstCard.addEventListener(MouseEvent.CLICK, checkCards); event.currentTarget.addEventListener(MouseEvent.CLICK, checkCards); _firstCard = undefined; } } } }
With the removeChild I get a error like this.
Error #2025: The supplied DisplayObject must be a child of the caller.
Tnx
-
The code you posted does not have removeChild in it. Nor do I see anything about gameover or restart.
The error you are getting is because you either called removeChild on the wrong parent, or with the wrong child.
-
Hello,
I'm sorry because I was trying all the time I removed it allready..
Actionscript Code:
_firstCard = undefined; _currentMatches ++; if(_currentMatches >= _totalMatches) { gotoAndStop(2); timer.stop(); removeChild(_card);
I know i dont have a restart in it yet, thats because I dont know how to do it at all...
Dont know where to begin. I'm really stuck at this point :-(
-
You could iterate through the displaylist and remove all the children which are Cards, but there's a better way. Each time you create a card and put it on the displayList, you put it into an array of your cards also. When you want to remove all cards, iterate through the array, removing each.
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
{
_cards = new Array();
_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;
}
}
}
}
Don't forget to reset your _cards array when you start a new game.
-
-
-
No dont know how to reset cards...
Timer wasnt that hard...
timer.reset();
and when you want to start it again
timer.start();
But still dont know how to reset the cards in a good way
-
Anybody? I know I'm almost done with my game... Only need the reset, but I really dont know how...?
If there is somebody that can help me out
That would be great
-
If you don't want to re-arrange the cards, then simply go through your _cards array and tell each card to go back to the initial frame or the flipback frame. If you DO want to shuffle your cards, or generate new ones, then you should
1. reset your currentmatches
2. call shuffleCards again
3. put your cards back on the display.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|