Exit
  • Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
  • 한국 커뮤니티
0

Removing/Resetting Timers for Game

New Here ,
May 30, 2014 May 30, 2014

Greetings fellow Flashdevelopers

My friend and I are trying to create a small Flash game for school but we're stuck trying to reset it when you have a Game Over.

The timers keep going on the Game Over screen and when you press the back button they keep going on the menu screen aswell.

We're trying to look for a solution to fix the issue.

We're also trying to find out how you make the game completely reset when you press the back button, so that you can start a new game without any issues.

package

{

  import flash.display.*;

  import flash.text.TextField;

  import flash.utils.Timer;

  import flash.events.*;

  import flash.ui.*;

  public class Main extends MovieClip

  {

  private var level:uint = 0;

  private var lives:int = 0;

  private var score:uint = 0

  private var speed:uint = 10;

  public function Main()

  {

  stop();

  }

  public function verwijderObstacles():void

  {

  if(getChildByName("obstacle_mc") != null){

  mcObstacle(getChildByName("obstacle_mc")).destroy();

  }

  }

  public function verwijderPowerups():void

  {

  if(getChildByName("score_mc") != null){

  mcScore(getChildByName("score_mc")).destroy();

  }

  }

  public function initGame()

  {

  stage.focus = stage;

  var gravity:Number = 3;

  var jumping:Boolean = false;

  var jumpPower:Number = 0;

  var score_mc:MovieClip = new mcScore();

  var obstacle_mc:MovieClip = new mcObstacle();

  if(!contains(score_mc))

  {

  var scoreTimer:Timer = new Timer(Math.ceil(Math.random() * 5) * 1000 ,1);

  scoreTimer.addEventListener(TimerEvent.TIMER, spawnScore);

  scoreTimer.start();

  }

  if(!contains(obstacle_mc))

  {

  var obstacleTimer:Timer = new Timer(Math.ceil(Math.random() * 2) * 1000 ,1);

  obstacleTimer.addEventListener(TimerEvent.TIMER, spawnObstacle);

  obstacleTimer.start();

  }

  stage.addEventListener(Event.ENTER_FRAME, scrollBackground);

  stage.addEventListener(Event.ENTER_FRAME, scrollClouds);

  stage.addEventListener(KeyboardEvent.KEY_DOWN, jump);

  stage.addEventListener(Event.ENTER_FRAME, update);

  stage.addEventListener(Event.ENTER_FRAME, moveScore);

  stage.addEventListener(Event.ENTER_FRAME, moveObstacle);

  stage.addEventListener(Event.ENTER_FRAME, hitScore);

  stage.addEventListener(Event.ENTER_FRAME, hitObstacle);

  stage.addEventListener(Event.ENTER_FRAME, checkOverlap);

  stage.addEventListener(Event.ENTER_FRAME, checkGame);

  function scrollBackground(e:Event):void

  {

  if(!(getChildByName("background1_mc") == null) && !(getChildByName("background2_mc") == null))

  {

  background1_mc.x -= speed;

  background2_mc.x -= speed;

  if(background1_mc.x <= -background1_mc.width)

  {

  background1_mc.x = background2_mc.x + background2_mc.width;

  }

  else if(background2_mc.x <= -background2_mc.width)

  {

  background2_mc.x = background1_mc.x + background1_mc.width;

  }

  }

  }

  function scrollClouds(e:Event):void

  {

  if(!(getChildByName("clouds1_mc") == null) && !(getChildByName("clouds2_mc") == null))

  {

  var cloudSpeed = speed / 5;

  clouds1_mc.x -= cloudSpeed;

  clouds2_mc.x -= cloudSpeed;

  if(clouds1_mc.x <= -clouds1_mc.width)

  {

  clouds1_mc.x = clouds2_mc.x + clouds2_mc.width;

  }

  else if(clouds2_mc.x <= -clouds2_mc.width)

  {

  clouds2_mc.x = clouds1_mc.x + clouds1_mc.width;

  }

  }

  }

  function jump(e:KeyboardEvent):void

  {

  if(e.keyCode == Keyboard.SPACE)

  {

  if(jumping != true)

  {

  jumpPower = -30;

  jumping = true;

  player_mc.gotoAndPlay(46);

  }

  }  

  }

  function update(e:Event):void

  {

  if(jumping)

  {

  player_mc.y += jumpPower;

  jumpPower += gravity;

  if(player_mc.y >= 320)

  {

  jumping = false;

  player_mc.y = 320;

  player_mc.gotoAndPlay(61);

  }

  }

  }

  function spawnScore(e:TimerEvent):void

  {

  var randomNumber:Number = Math.round(Math.random());

  addChild(score_mc);

  score_mc.name = "score_mc";

  if(randomNumber == 0)

  {

  score_mc.x = 800;

  score_mc.y = 240;

  }

  else

  {

  score_mc.x = 800;

  score_mc.y = 30;

  }

  }

  function spawnObstacle(e:TimerEvent):void

  {

  addChild(obstacle_mc);

  obstacle_mc.x = 800;

  obstacle_mc.y = 240;

  obstacle_mc.name = "obstacle_mc";

  }

  function moveScore(e:Event):void

  {

  score_mc.x -= speed;

  if(!(getChildByName("score_mc") == null))

  {

  if(score_mc.x < -86)

  {

  removeChild(getChildByName("score_mc"));

  var scoreTimer:Timer = new Timer(Math.ceil(Math.random() * 5) * 1000 ,1);

  scoreTimer.addEventListener(TimerEvent.TIMER, spawnScore);

  scoreTimer.start();

  }

  }

  }

  function moveObstacle(e:Event):void

  {

  obstacle_mc.x -= speed;

  if(!(getChildByName("obstacle_mc") == null))

  {

  if(obstacle_mc.x < -72)

  {

  removeChild(getChildByName("obstacle_mc"));

  var obstacleTimer:Timer = new Timer(Math.ceil(Math.random() * 5) * 1000 ,1);

  obstacleTimer.addEventListener(TimerEvent.TIMER, spawnObstacle);

  obstacleTimer.start();

  }

  }

  }

  function hitScore(e:Event):void

  {

  if(!(getChildByName("score_mc") == null) && !(getChildByName("player_mc") == null))

  {

  if (player_mc.hitTestObject(score_mc))

  {

  removeChild(getChildByName("score_mc"));

  var scoreTimer:Timer = new Timer(Math.ceil(Math.random() * 5) * 1000 ,1);

  scoreTimer.addEventListener(TimerEvent.TIMER, spawnScore);

  scoreTimer.start();

  player_mc.gotoAndPlay(2);

  score++;

  if(score == 1)

  {

  gauge_mc.gotoAndPlay(2);

  }

  else if(score == 2)

  {

  gauge_mc.gotoAndPlay(16);

  }

  else if(score == 3)

  {

  gauge_mc.gotoAndPlay(31);

  }

  else if(score == 4)

  {

  gauge_mc.gotoAndPlay(46);

  }

  else if(score == 5)

  {

  gauge_mc.gotoAndPlay(61);

  }

  }

  }

  }

  function hitObstacle(e:Event):void

  {

  if(!(getChildByName("obstacle_mc") == null) && !(getChildByName("player_mc") == null))

  {

  if(player_mc.hitTestPoint(obstacle_mc.x, obstacle_mc.y, true))

  {

  removeChild(getChildByName("obstacle_mc"));

  var obstacleTimer:Timer = new Timer(Math.ceil(Math.random() * 5) * 1000 ,1);

  obstacleTimer.addEventListener(TimerEvent.TIMER, spawnObstacle);

  obstacleTimer.start();

  player_mc.gotoAndPlay(16);

  lives--;

  lives_txt.text = "x" + lives.toString();

  }

  }

  }

  function checkOverlap(e:Event):void

  {

  if(obstacle_mc.hitTestObject(score_mc))

  {

  if(!(getChildByName("score_mc") == null))

  {

  removeChild(getChildByName("score_mc"));

  var scoreTimer:Timer = new Timer(Math.ceil(Math.random() * 5) * 1000 ,1);

  scoreTimer.addEventListener(TimerEvent.TIMER, spawnScore);

  scoreTimer.start();

  }

  }

  }

  function checkGame(e:Event):void

  {

  if(lives < 0)

  {

  scoreTimer.stop();

  obstacleTimer.stop();

  if(getChildByName("score_mc") != null){

  mcScore(getChildByName("score_mc")).destroy();

  }

  gotoAndStop("gameover");

  }

  }

  }

  }

}

This is the AS3 script we wrote for the game. I highlighted the places in red where we tried to write some code for the Game Over screen.

TOPICS
ActionScript
469
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines

correct answers 1 Correct answer

LEGEND , May 30, 2014 May 30, 2014

You have a lot of ENTER_FRAME event listeners at work, at least one of which is telling the Timer to start().  YOu need to remove them if you want to start fresh and stop the Timer(s).

Translate
LEGEND ,
May 30, 2014 May 30, 2014

You have a lot of ENTER_FRAME event listeners at work, at least one of which is telling the Timer to start().  YOu need to remove them if you want to start fresh and stop the Timer(s).

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
May 30, 2014 May 30, 2014

Alright! That works. Is there a way to delete the objects that are on the screen? I removed the ENTER_FRAME events in the red part of the code.

I have some issues with the music aswel. It just replays the track over the first one, so the track is playing 2 times at the same time.

Thanks for the help Ned, We really Appreciate it.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
May 30, 2014 May 30, 2014
LATEST

If the objects you want to remove were added dynamically then remove them dynamically using removeChild() and null any references to them.

As far as sound goes, if you use the Sound class you should have complete control over when sound plays and when it doesn't.... if you are placing sounds in the timeline, not so much control is possible..

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines