Get the full commented source code of

HTML5 Suika Watermelon Game

Talking about Actionscript 3 and Flash.

Not so long ago I showed you how to pause a Flash game or movie detecting its focus using ACTIVATE and DEACTIVATE events.

Unfortunately a new problem comes when you have Timer events.

Although you may think you just need to stop and restart the timer, the internal counter resets when you start it again.

So let’s say you have a timer event you have to trigger at every second, if you pause the game, and then unpause and pause it again in less than a second, the timer will never be triggered.

The solution is to keep track of how much time passed since the last tick and the pause, and when the user unpauses the movie, setting the delay of the first timer to be triggered to the difference between the normal delay and the difference between the normal delay itself and the time passed since the last tick.

This script shows the difference between pausing a “normal” and a “smart” timer:

package {
	import flash.display.Sprite;
	import flash.utils.Timer;
	import flash.utils.getTimer;
	import flash.events.TimerEvent;
	import flash.text.TextField;
	import flash.text.TextFormat;
	import flash.events.Event;
	import flash.text.TextFieldAutoSize;
	public class pauseTimer extends Sprite {
		// the interval between two ticks, in milliseconds
		private var interval:int=1000;
		// the "normal" timer
		private var normalTimer:Timer;
		// the "smart" timer
		private var smartTimer:Timer;
		// just some text field related variables
		private var normalTimerText:TextField = new TextField();
		private var smartTimerText:TextField = new TextField();
		private var normalTextFormat:TextFormat = new TextFormat();
		private var smartTextFormat:TextFormat = new TextFormat();
		// the "movie paused" screen
		private var pausedScreen:pausedMc=new pausedMc();
		// variables used to handle the smart timer
		private var lapTime:Number=0;
		private var currentTimer:Number=0;
		public function pauseTimer() {
			// nothing interesting, only some layout settings
			normalTextFormat.size=24;
			normalTextFormat.color=0xA90000;
			smartTextFormat.size=24;
			smartTextFormat.color=0x00A900;
			addChild(normalTimerText);
			normalTimerText.autoSize=TextFieldAutoSize.LEFT;
			addChild(smartTimerText);
			smartTimerText.autoSize=TextFieldAutoSize.LEFT;
			addChild(pausedScreen);
			smartTimerText.y=50;
			// timer initialization
			normalTimer=new Timer(interval);
			smartTimer=new Timer(interval);
			// timer listeners
			normalTimer.addEventListener(TimerEvent.TIMER, showNormalTime);
			smartTimer.addEventListener(TimerEvent.TIMER, showSmartTime);
			// activate/deactivate listeners
			addEventListener(Event.ACTIVATE,onActivate);
			addEventListener(Event.DEACTIVATE,onDeactivate);
		}
		private function showNormalTime(e:TimerEvent):void {
			// just showing the number of ticks
			normalTimerText.text="Normal counter: "+normalTimer.currentCount+" ticks counted";
			normalTimerText.setTextFormat(normalTextFormat);
		}
		private function showSmartTime(e:TimerEvent):void {
			// set the delay to "interval" value if it has a different value (it will happen)
			if (smartTimer.delay!=interval) {
				smartTimer.delay=interval;
			}
			// saving the current timer
			currentTimer=getTimer();
			smartTimerText.text="Smart counter: "+smartTimer.currentCount+" ticks counted";
			smartTimerText.setTextFormat(smartTextFormat);
		}
		private function onActivate(e:Event):void {
			pausedScreen.visible=false;
			// smart timer delay management
			if (smartTimer.delay-lapTime>0) {
				smartTimer.delay=smartTimer.delay-lapTime;
			}
			// saving the curren time
			currentTimer=getTimer();
			// starting the timers
			normalTimer.start();
			smartTimer.start();
		}
		private function onDeactivate(e:Event):void {
			pausedScreen.visible=true;
			// stopping the timers
			normalTimer.stop();
			smartTimer.stop();
			// determining how much time has passed since the last tick
			lapTime=(getTimer()-currentTimer);
			smartTimerText.text="Smart counter: "+smartTimer.currentCount+" ticks counted and "+(smartTimer.delay-lapTime)+"ms to next tick";
			smartTimerText.setTextFormat(smartTextFormat);
		}
	}
}

And this is the result:

Try to pause and un pause the movie very quickly, clicking inside and outside it, to see how the normal timer never gets triggered while the smart timer does.

Download the source code.

Never miss an update! Subscribe, and I will bother you by email only when a new game or full source code comes out.