Skip to main content
Inspiring
May 17, 2013
Question

Game breaks after first play?

  • May 17, 2013
  • 1 reply
  • 1100 views

I don't really understand how this happens, but after you run my game once, on any difficulty, it won't work right again. What is supposed to happen, when you enter the game, it sets a variable to true. Then when you click your button, it starts the game. That works for the first run only.

If you run it again (by clicking the main menu button and starting the game on a different difficulty) the game instantly starts.

I can't seem to figure out how this happens, but here's my code:

import flash.events.Event;

import flash.utils.Timer;

import flash.events.MouseEvent;

stop();

var allowBegin:Boolean = false;

var clickCheck:Boolean = false;

var compWin:Boolean = false;

var playerWin:Boolean = false;

var playerNum:int = 0;

var compNum:int = 0;

clickMe.addEventListener(MouseEvent.MOUSE_DOWN, addPlayerHit);

clickMe.addEventListener(MouseEvent.MOUSE_UP, resetClick);

stage.addEventListener(MouseEvent.MOUSE_DOWN, allowStart);

compClock.addEventListener(TimerEvent.TIMER, computerFunct);

mcCompArm.gotoAndStop(1);

clickMe.gotoAndStop(1);

compClick.gotoAndStop(1);

btn_gameOver.visible = false;

setDiff();

function allowStart(Mouse:Event):void

{

          allowBegin = true;

          stage.removeEventListener(MouseEvent.MOUSE_DOWN, allowStart);

          stage.addEventListener(MouseEvent.MOUSE_DOWN, startTimer);

          trace("Used");

}

function computerFunct(e:TimerEvent):void

{

          addCompHit();

    compClock.reset();

          compClock.start();

}

function startTimer(Mouse:Event):void

{

          if(allowBegin == true){

                    compClock.start();

                    allowBegin = false;

          }

}

function addPlayerHit(Mouse:Event):void

{

          if( playerNum < 100 && clickCheck == false){

                    playerNum++;

                    playerCounter.counterText.text = String(playerNum);

                    clickMe.gotoAndStop(2);

                    clickCheck = true;

          }else if(playerNum == 99 && clickCheck == false && playerWin == false && compWin == false)

          {

                    setDiffBeat();

                    playerCounter.counterText.text = "WIN";

                    compCounter.counterText.text = "LOS";

                    endGame();

          }else if (compWin == true){

                    setDiffLost();

                    playerCounter.counterText.text = "LOS";

                    compCounter.counterText.text = "WIN";

                    endGame();

          }

}

function resetClick(Mouse:Event):void{

          if(clickCheck == true){

                    clickMe.gotoAndStop(1);

                    clickCheck = false;

          }

}

function addCompHit():void

{

          if( compNum < 100 && playerWin == false){

                    compNum++;

                    compCounter.counterText.text = String(compNum);

          }else if(compNum == 100 && playerWin == false && compWin == false)

          {

                    setDiffLost();

                    playerCounter.counterText.text = "LOS";

                    compCounter.counterText.text = "WIN";

                    endGame();

          }else if (playerWin == true){

                    setDiffBeat();

                    playerCounter.counterText.text = "WIN";

                    compCounter.counterText.text = "LOS";

                    endGame();

          }

}

function endGame():void

{

          clickMe.removeEventListener(MouseEvent.MOUSE_DOWN, addPlayerHit);

          clickMe.removeEventListener(MouseEvent.MOUSE_UP, resetClick);

          compClock.removeEventListener(TimerEvent.TIMER, computerFunct);

          stage.removeEventListener(MouseEvent.MOUSE_DOWN, allowStart);

          stage.removeEventListener(MouseEvent.MOUSE_DOWN, startTimer);

          btn_gameOver.addEventListener(MouseEvent.MOUSE_DOWN, resetGame);

          compClick.gotoAndStop(1);

          mcCompArm.gotoAndStop(1);

          compWin = true;

          allowBegin = false;

          btn_gameOver.visible = true;

}

function resetGame(Mouse:Event):void

{

          allowBegin = false;

          clickCheck = false;

          compWin = false;

          playerWin = false;

          playerNum = 0;

          compNum = 0;

          allowBegin = false;

          btn_gameOver.visible = false;

          btn_gameOver.removeEventListener(MouseEvent.MOUSE_DOWN, resetGame);

          gotoAndStop("menu");

}

and some more here:

import flash.text.TextFormat;

var difficulty:String = "";

var compClock:Timer=new Timer(999);

var diffBeat:Array = ["no", "no", "no", "no", "no", "no"];

var beatText:TextFormat = new TextFormat();

beatText.color = 0x00FF22;

var lostText:TextFormat = new TextFormat();

lostText.color = 0xFF0022;

function setDiff():void

{

          switch(difficulty){

                    case "superEasy":

                              compClock.delay = 500;

                    break;

                    case "easy":

                              compClock.delay = 400;

                    break;

                    case "normal":

                              compClock.delay = 250;

                    break;

                    case "hard":

                              compClock.delay = 170;

                    break;

                    case "superHard":

                              compClock.delay = 120

                    break;

                    case "impossible":

                              compClock.delay = 60;

                    break;

                    default:

                              compClock.delay = 900;

                              trace("Error");

                    break;

          }

}

function setDiffBeat():void

{

          switch(difficulty){

                    case "superEasy":

                              diffBeat[0] = "yes";

                    break;

                    case "easy":

                              diffBeat[1] = "yes";

                    break;

                    case "normal":

                              diffBeat[2] = "yes";

                    break;

                    case "hard":

                              diffBeat[3] = "yes";

                    break;

                    case "superHard":

                              diffBeat[4] = "yes";

                    break;

                    case "impossible":

                              diffBeat[5] = "yes";

                              trace("Beat");

                    break;

          }

}

function showDiffBeat():void

{

          if(diffBeat[0] == "yes"){

                    supEasy_txt.setTextFormat(beatText);

          }

          if(diffBeat[1] == "yes"){

                    easy_txt.setTextFormat(beatText);

          }

          if(diffBeat[2] == "yes"){

                    norm_txt.setTextFormat(beatText);

          }

          if(diffBeat[3] == "yes"){

                    hard_txt.setTextFormat(beatText);

          }

          if(diffBeat[4] == "yes"){

                    supHard_txt.setTextFormat(beatText);

          }

          if(diffBeat[5] == "yes"){

                    impossible_txt.setTextFormat(beatText);

                    trace("Added");

          }

          if(diffBeat[0] == "lost"){

                    supEasy_txt.setTextFormat(lostText);

          }

          if(diffBeat[1] == "lost"){

                    easy_txt.setTextFormat(lostText);

          }

          if(diffBeat[2] == "lost"){

                    norm_txt.setTextFormat(lostText);

          }

          if(diffBeat[3] == "lost"){

                    hard_txt.setTextFormat(lostText);

          }

          if(diffBeat[4] == "lost"){

                    supHard_txt.setTextFormat(lostText);

          }

          if(diffBeat[5] == "lost"){

                    impossible_txt.setTextFormat(lostText);

          }

}

function setDiffLost():void

{

          switch(difficulty){

                    case "superEasy":

                              diffBeat[0] = "lost";

                    break;

                    case "easy":

                              diffBeat[1] = "lost";

                    break;

                    case "normal":

                              diffBeat[2] = "lost";

                    break;

                    case "hard":

                              diffBeat[3] = "lost";

                    break;

                    case "superHard":

                              diffBeat[4] = "lost";

                    break;

                    case "impossible":

                              diffBeat[5] = "lost";

                    break;

          }

}

This topic has been closed for replies.

1 reply

kglad
Community Expert
Community Expert
May 17, 2013

which button is clicked to restart the game?

what's supposed to happen when that button is clicked?

Inspiring
May 17, 2013

this is the function called to reset the game:

function resetGame(Mouse:Event):void

{

          allowBegin = false;

          clickCheck = false;

          compWin = false;

          playerWin = false;

          playerNum = 0;

          compNum = 0;

          allowBegin = false;

          btn_gameOver.visible = false;

          btn_gameOver.removeEventListener(MouseEvent.MOUSE_DOWN, resetGame);

          gotoAndStop("menu");

}

and it's being used/called by btn_gameOver, which only show's up when the game ends.

Inspiring
May 17, 2013

instead of the game instantly starting when btn_gameOver is clicked, what's supposed to happen?

and, do you have code on more than one timeline and/or frame?


Yeah I have some code that is on all 5 frames, but it's just one long keyframe. It's the second set of code that's posted above.

And the game isn't supposed to start when btn_gameOver is clicked. That takes you back to the main menu. You're supposed to be able to start the game as soon as you click anywhere on the stage after entering. But it starts instantly after you've played once through.