Hello,
I am working on a flash matching game which will be ported to the iPad later on…
However, i am sort of stuck now:shifty:…
I hope there are some people who could help me with this problem: i am trying to restart the game when a button is pressed (restartbttn.addEventListener(MouseEvent.CLICK, restartGame) but i seem to be forgetting something because the game does restart but the function that checks for a match doesn’t work anymore…
here is my code to clear things up (i hope:})
package {
import flash.events.Event;
import flash.net.URLRequest;
import flash.display.MovieClip;
import flash.display.Loader;
import flash.events.MouseEvent;
import flash.media.SoundChannel;
import flash.media.Sound;
public class game extends MovieClip{
public var firstRun:Boolean = new Boolean(true);
public var instButton1:MovieClip = new MovieClip();
public var instButton2:MovieClip = new MovieClip();
public var moHolder:MovieClip = new MovieClip();
public var gamebg:gameBG = new gameBG();
public static var _self:MovieClip = new MovieClip();
public var instr1:inst1 = new inst1();
public var instr2:inst2 = new inst2();
public var instr3:inst3 = new inst3();
public var instr4:inst4 = new inst4();
public var instr5:inst5 = new inst5();
public var instr6:inst6 = new inst6();
public var moi1:mo1 = new mo1();
public var moi2:mo2 = new mo2();
public var moi3:mo3 = new mo3();
public var moi4:mo4 = new mo4();
public var moi5:mo5 = new mo5();
public var moi6:mo6 = new mo6();
public var scorebalk:scoreBar = new scoreBar();
public var stopbttn:end = new end();
public var restartbttn:restart = new restart();
public var instVector:Vector.<MovieClip> = new Vector.<MovieClip>();
public var shuffledInstVector:Vector.<MovieClip> = new Vector.<MovieClip>();
public var moVector:Vector.<MovieClip> = new Vector.<MovieClip>();
public var shuffledMoVector:Vector.<MovieClip> = new Vector.<MovieClip>();
public var shuffledOrder:Vector.<Number> = new Vector.<Number>();
public static var score:Number = new Number(0);
public var i:Number = new Number(0);
public var n:Number = new Number(1);
public var currentInst:Number;
public var currentInst1:Number;
public var currentInst2:Number;
//sounds for score
var sndchan1:SoundChannel = new SoundChannel();
var snd1:Sound = new score1();
var sndchan2:SoundChannel = new SoundChannel();
var snd2:Sound = new score2();
var sndchan3:SoundChannel = new SoundChannel();
var snd3:Sound = new score3();
var sndchan4:SoundChannel = new SoundChannel();
var snd4:Sound = new score4();
var sndchan5:SoundChannel = new SoundChannel();
var snd5:Sound = new score5();
var sndchan6:SoundChannel = new SoundChannel();
var snd6:Sound = new score6();
public function game() {
_self = MovieClip(this);
gameStart();
// constructor code
}
function randomNumber(min:Number, max:Number):Number {
var randomNum:Number = Math.floor(Math.random() * (max - min + 1)) + min;
return randomNum;
}
public function gameStart()
{
score = 0;
Main.gameHolder.addChild(gamebg);
Main.gameHolder.addChild(moHolder);
Main.gameHolder.addChild(instButton1);
Main.gameHolder.addChild(instButton2);
Main.gameHolder.addChild(stopbttn);
Main.gameHolder.addChild(scorebalk);
Main.gameHolder.addChild(restartbttn);
restartbttn.x = 1024;
restartbttn.y = 768;
stopbttn.y = 768;
scorebalk.x = 512;
scorebalk.y = 681;
scorebalk.stop();
setupMo();
setupInstruments();
restartbttn.addEventListener(MouseEvent.CLICK, restartGame);
}
public function setupInstruments()
{
instr1.name = "1";
instr2.name = "2";
instr3.name = "3";
instr4.name = "4";
instr5.name = "5";
instr6.name = "6";
instr1.id = 1;
instr2.id = 2;
instr3.id = 3;
instr4.id = 4;
instr5.id = 5;
instr6.id = 6;
trace("inst: "+instVector);
currentInst = randomNumber(1,3);
instButton1.addChild(shuffledInstVector*);//bepaal hier * het hoeveelste item uit de vector
instButton2.addChild(shuffledInstVector[i+currentInst]);
if(randomNumber(1,2) == 1) {
trace("1st: "+shuffledOrder*);
shuffledInstVector*.x = 460;
shuffledInstVector[i+currentInst].x = 564;
} else {
trace("2nd: "+shuffledOrder*);
shuffledInstVector[i+currentInst].x = 460;
shuffledInstVector*.x = 564;
}
shuffledInstVector*.y = 50;
shuffledInstVector[i+currentInst].y = 50;
shuffledInstVector*.addEventListener(MouseEvent.MOUSE_DOWN, dragInstruments);//set up voor t drag script
shuffledInstVector*.addEventListener(MouseEvent.MOUSE_UP, dropInstruments);
shuffledInstVector[i+currentInst].addEventListener(MouseEvent.MOUSE_DOWN, dragInstruments);
shuffledInstVector[i+currentInst].addEventListener(MouseEvent.MOUSE_UP, dropInstruments);
}
public function setupMo()
{
moi1.name = "1";
moi2.name = "2";
moi3.name = "3";
moi4.name = "4";
moi5.name = "5";
moi6.name = "6";
moi1.id = 1;
moi2.id = 2;
moi3.id = 3;
moi4.id = 4;
moi5.id = 5;
moi6.id = 6;
moVector.push(moi1, moi2, moi3, moi4, moi5, moi6);//schrijf hier wat er in de vector moet
instVector.push(instr1, instr2, instr3, instr4, instr5, instr6);
while (moVector.length > 0)//zorgt voor het random neerzetten van een item
{
var wichMo:Number = Math.floor(Math.random()*moVector.length);
shuffledMoVector.push(moVector[wichMo]);
shuffledInstVector.push(instVector[wichMo]);
shuffledOrder.push(moVector[wichMo].id);
instVector.splice(wichMo, 1);
moVector.splice(wichMo, 1);
}
shuffledInstVector.push(shuffledInstVector[0],shuffledInstVector[1],shuffledInstVector[2]);
trace(shuffledInstVector);
trace(shuffledMoVector);
trace(shuffledOrder);
moHolder.addChild(shuffledMoVector*);//bepaal hier * het hoeveelste item uit de vector
moHolder.x = 512;
moHolder.y = 568;
}
public function dragInstruments(evt:MouseEvent)
{
var object = evt.target;
object.X = object.x;
object.Y = object.y;
object.startDrag();
}
public function dropInstruments(evt:MouseEvent)
{
trace(evt.target.id + " ");
var object = evt.target;
object.stopDrag();
if (evt.target.hitTestObject(moHolder)) //raakt mo
{
//functie check for the same instrument
if (evt.target.id == shuffledOrder*)//klopt met mo
{
goodChoice();
} else {
//foute feedback speelt
object.x = object.X;
object.y = object.Y;
}
}
else//mist mo
{
object.x = object.X;
object.y = object.Y;
}
}
public function goodChoice()
{
//speel goede feedback
//pauzeer met instrument in goede houding
//doe dan wat hieronder staat
score += 1;
scorebalk.gotoAndStop(score+1);
moHolder.removeChild(shuffledMoVector*);
instButton1.removeChild(shuffledInstVector*);
instButton2.removeChild(shuffledInstVector[i+currentInst]);
i += 1;
n += 1;
currentInst = randomNumber(1,3);
moHolder.addChild(shuffledMoVector*);
instButton1.addChild(shuffledInstVector*);//bepaal hier * het hoeveelste item uit de vector
instButton2.addChild(shuffledInstVector[i+currentInst]);
if(randomNumber(1,2) == 1) {
trace("1st: "+shuffledOrder*);
shuffledInstVector*.x = 460;
shuffledInstVector[i+currentInst].x = 564;
} else {
trace("2nd: "+shuffledOrder*);
shuffledInstVector[i+currentInst].x = 460;
shuffledInstVector*.x = 564;
}
shuffledInstVector*.y = 50;
shuffledInstVector[i+currentInst].y = 50;
shuffledInstVector*.addEventListener(MouseEvent.MOUSE_DOWN, dragInstruments);//set up voor t drag script
shuffledInstVector*.addEventListener(MouseEvent.MOUSE_UP, dropInstruments);
shuffledInstVector[i+currentInst].addEventListener(MouseEvent.MOUSE_DOWN, dragInstruments);
shuffledInstVector[i+currentInst].addEventListener(MouseEvent.MOUSE_UP, dropInstruments);
}
public static function stopGame()
{
//show mo UI
Main.stopGame();
}
public function restartGame(evt:MouseEvent)
{
trace("restart");
scorebalk.gotoAndStop(0);
score = 0;
Main.gameHolder.removeChild(gamebg);
Main.gameHolder.removeChild(moHolder);
Main.gameHolder.removeChild(instButton1);
Main.gameHolder.removeChild(instButton2);
Main.gameHolder.removeChild(stopbttn);
Main.gameHolder.removeChild(scorebalk);
Main.gameHolder.removeChild(restartbttn);
restartbttn.removeEventListener(MouseEvent.CLICK, restartGame);
shuffledInstVector*.removeEventListener(MouseEvent.MOUSE_DOWN, dragInstruments);//set up voor t drag script
shuffledInstVector*.removeEventListener(MouseEvent.MOUSE_UP, dropInstruments);
shuffledInstVector[i+currentInst].removeEventListener(MouseEvent.MOUSE_DOWN, dragInstruments);
shuffledInstVector[i+currentInst].removeEventListener(MouseEvent.MOUSE_UP, dropInstruments);
i=0;
n=1;
moHolder.removeChild(shuffledMoVector*);
instButton1.removeChild(shuffledInstVector*);
instButton2.removeChild(shuffledInstVector[i+currentInst]);
shuffledMoVector.length = 0;
shuffledInstVector.length = 0;
//wichMo = 0;
//shuffledInstVector = null;
//shuffledMoVector = null;
gameStart();
}
}
}