[AS3] Restarting Timers upon Game Restart

8 posts

Flag Post

Hello,

I am currently creating an avoider game in AS3. This is my first programming project.

I am having issues finding a solution for restarting timers, more specifically timers which have a delay on, when I restart the game.

20 seconds into the game (using a delay) a “shar” object spawns and will consistently spawn every 2 seconds from now on. However if you die after this object has started to spawn and restart the game, the shar objects will continue spawning 3 seconds into starting the new game. My intention is to have the timers completely reset so the shar object will consistently spawn 20 seconds into the game on a new game, instead of only on the first game.

Code from Game.as:

package 
{

	import flash.display.MovieClip;
	import flash.events.Event;
	import flash.events.TimerEvent;
	import flash.utils.Timer;
	import flash.text.TextField;
	import flash.text.TextFormat;

	public class Game extends MovieClip
	{

		static var player:MovieClip;
		static var orbTimer:Timer;
		static var sharTimer:Timer;
		static var lightningTimer:Timer;
		static var healthMeter:HealthMeter;
		static var gameOverMenu:GameOverMenu;
		static var wardTimer:Timer;
		static var burstTimer:Timer;
		static var seconds:Number = 0;


		function Game()
		{

			Key.initialize(stage);
			player = new Player();
			player.x = 480;
			player.y = 500;
			addChild(player);

			orbTimer = new Timer(250);
			orbTimer.addEventListener("timer", sendOrb);
			orbTimer.start();

			sharTimer = new Timer(2000);
			sharTimer.addEventListener("timer", sendShar);
			sharTimer.delay = 20000;
			sharTimer.start();			
			
			lightningTimer = new Timer(3000);
			lightningTimer.addEventListener("timer", sendLightning);
			lightningTimer.delay = 40000;
			lightningTimer.start();

			wardTimer = new Timer(20000);
			wardTimer.addEventListener("timer", sendWard);
			wardTimer.delay = 20000
			wardTimer.start();

			burstTimer = new Timer(40000);
			burstTimer.addEventListener("timer", sendBurst);
			burstTimer.delay = 40000
			burstTimer.start();

			healthMeter = new HealthMeter();
			healthMeter.x = 350;
			healthMeter.y = 620;
			addChild(healthMeter);

			gameOverMenu = new GameOverMenu();
			gameOverMenu.x = 480;
			gameOverMenu.y = 320;
			addChild(gameOverMenu);
			gameOverMenu.visible = false;
			gameOverMenu.playAgainButton.addEventListener("mouseDown", newGame);

			player.shield.visible = false;

		}

		static function gameOver()
		{

						
			gameOverMenu.visible = true;

			orbTimer.reset();
			sharTimer.reset();
			lightningTimer.reset();
			wardTimer.reset();
			burstTimer.reset();
			
			
			Game.player.shield.visible = false;
			
		}

		function newGame(e:Event)
		{

			gameOverMenu.visible = false;
			player.visible = true;
			player.x = 480;
			player.y = 500;
			player.takeDamage(-player.maxHealth);
			player.addEventListener("enterFrame", player.move);

			orbTimer.start();
			sharTimer.start();
			lightningTimer.start();
			wardTimer.start();
			burstTimer.start();

			Game.player.shield.visible = false;

		}

		function sendWard(e:Event)
		{

			var ward = new Ward();
			wardTimer.delay = 40000;
			stage.addChild(ward);

		}

		function sendBurst(e:Event)
		{

			var burst = new Burst();
			burstTimer.delay = 40000;
			stage.addChild(burst);

		}

		function sendOrb(e:Event)
		{

			var enemy = new Orb();
			stage.addChild(enemy);

		}

		function sendShar(e:Event)
		{

			var shar = new Shar();
			sharTimer.delay = 2000;
			stage.addChild(shar);


		}

		function sendLightning(e:Event)
		{

			var lightning = new Lightning();
			lightningTimer.delay = 3000;
			stage.addChild(lightning);

		}





	}

}
 
Flag Post

RTFM

 
Flag Post

I’ve never used timers, don’t even know how they work, I just make a number and have it increase every frame, when it’s above 100 for example I execute a function which sets it to 0, and it just keeps increasing.

 
Flag Post
Originally posted by DrYoshiyahu:
I just make a number and have it increase every frame

Which is the right way to do it.

Also, wtf with the static nightmare?

 
Flag Post
Originally posted by Senekis93:
Originally posted by DrYoshiyahu:
I just make a number and have it increase every frame

Which is the right way to do it.

Also, wtf with the static nightmare?

To be fair, timers are appropriate for some things too.

 
Flag Post
Originally posted by BobTheCoolGuy:
Originally posted by Senekis:
Originally posted by DrYoshiyahu:
I just make a number and have it increase every frame

Which is the right way to do it.

Also, wtf with the static nightmare?

To be fair, timers are appropriate for some things too.

Yeah, but not for real time games.
Focus and press space.

 
Flag Post

Before resetting the sharTimer, readjust it’s delay :D

 
Flag Post

Why didn’t I think of that before?

Thanks a lot!