TimerManger for Actopn Script 3.0 – 100 Useful classes

January 6th, 2012 by aabhaanjan

dremsus.com
Lets handle showing Timer in your game:
[xml]

//

package
{
/*
* @Comment: This class is for showing timer in your game or application.
*
*/
import flash.display.MovieClip;
import flash.events.Event;
import flash.text.TextField;
import flash.utils.Timer;
import flash.events.TimerEvent;

public class TimerManager extends MovieClip
{
private var _timer:Timer;
private var _interval:int;
private var _currentTime:String;
private var _currentCount;
private var _intialTime:int=0;
private var _targetTime:int;
private var _textField:TextField;
/*
* @Params:
* @__type:There are two types of timer you can use one is in increasing order while other is in decreasing order
* @Pass ‘DECREASE’ for decreasing order and leave blank for increasing order.
* @__timerInterval: This is the interval between the increment.
* @__targetTime : The complete time where the thmer has to reach.
* @__textField: The text field where you want to show the Time.
*
*/
public function TimerManager(__type:String,__timerInterval:int,__targetTime:int,__textField:TextField){
_interval = __timerInterval;
_targetTime = __targetTime;
_textField = __textField;
addTimer(__type);
}

private function addTimer(__type:String = “”):void {
_timer = new Timer(_interval);
_timer.start();
if(__type == ‘DECREASE’){
_timer.addEventListener(TimerEvent.TIMER, decreaseTimer);
}else {
_timer.addEventListener(TimerEvent.TIMER, increaseTimer);
}
}

private function decreaseTimer(evt:Event):void{
_currentCount = evt.target.currentCount;
_intialTime = _targetTime – evt.target.currentCount;
_textField.text = calculateTimeInDecre();
}

private function calculateTimeInDecre():String {
var __sec = _intialTime;
var __min = Math.floor(__sec/60);
__sec = String(__sec % 60);
if(__sec.length < 2){ __sec = "0" + __sec; } __min = String(__min % 60); if(__min.length < 2){ __min = "0" + __min; } _currentTime = __min + ":" + __sec; if(_currentTime == "00:00"){ _timer.stop(); } return _currentTime; } private function increaseTimer(evt:Event):void{ _intialTime += 1 _textField.text = calculateTimeInIncre(); } private function calculateTimeInIncre():String { var __sec = _intialTime; var __min = Math.floor(__sec/60); __sec = String(__sec % 60); if(__sec.length < 2){ __sec = "0" + __sec; } __min = String(__min % 60); if(__min.length < 2){ __min = "0" + __min; } _currentTime = __min + ":" + __sec; return _currentTime; } /* * @Comment: If you want to stop the timer in between call this function. * */ public function stopTimer():void { _timer.stop(); } } } [/xml]