Announcement

Collapse

The Infinity Blade Forums Have Moved

We've launched brand new Infinity Blade forums with improved features and revamped layout. We've also included a complete archive of the previous posts. Come check out the new Infinity Blade forums.
See more
See less

Scaleform timer example

Collapse
X
  • Filter
  • Time
  • Show
Clear All
new posts

  • Scaleform timer example

    Hello everyone,

    As part of my project I wanted to create a lab timer that would record seconds, minutes, and hours. I created a file in Flash CS3 Professional that uses Actionscript 2 code (Provided below).

    Code:
    //timerInterval contains the identifier for our time interval and timer contains
    //the current value of our timer. timerDelayMillisecons is set to a value of 1000 (one second)
    //timerState is a flag to hold whether or not our timer is currently running. 
    //our TimerStateIndictator is a movieclip with a TimerStop and TimerStart animation frames.
    //The TimerStart animation frame is a green circle, and the TimerStop animation frame is a red circle.
    timerInterval = 0;
    timerValue = 0;
    timerDelayMilliseconds = 1000;
    timerState = false;
    TimerStateIndicator.gotoAndStop("TimerStop");
    
    keyListener = new Object();
    keyListener.onKeyDown = function() {
    	//if we press the 't' key toggle the timer
    	if(Key.getCode() == 84) {
    		timerToggle();
    	}
    	//if we press the 'r' key reset the timer
    	if(Key.getCode() == 82) {
    		resetTimer();
    	}
    }
    Key.addListener(keyListener);
    
    function timerToggle(){
    	if(!timerState){
    		startTimer();
    	}
    	else{
    		stopTimer();
    	}
    }
    
    function startTimer(){
    	clearInterval(timerInterval);
    	timerInterval = setInterval(timerTick, timerDelayMilliseconds);
    	timerState = true;
    	TimerStateIndicator.gotoAndStop("TimerStart");
    	//communicate with kismet that our timer started
    	fscommand("timerStart");
    }
    
    function stopTimer(){
    	clearInterval(timerInterval);
    	timerState = false;
    	TimerStateIndicator.gotoAndStop("TimerStop");
    	//communicate with kismet that our timer stopped and pass the time
    	fscommand("timerStop", timerDisplay.timerText.text);
    }
    
    function resetTimer(){
    	timerValue = 0;
    	displayTime();
    	//communicate with kismet that our timer has been reset
    	fscommand("timerReset");
    }
    
    function timerTick(){
    	timerValue++;
    	displayTime();
    }
    
    //This function helps to format a numerical value by putting leading zeros
    //if the value is smaller than the number of digits required. For instance
    //if the value to display is a 9 and the number of digits required is two,
    //this function will test to see if the number to pad is less than 10. If it is
    //we will add a leading zero.
    function padZero(numToPad, numDigits){
    	retStr = "";
    	
    	if(numDigits < 2)
    		return String(numToPad);
    		
    	for(zeroIdx = 0; zeroIdx < numDigits; zeroIdx++){
    		if(numToPad < zeroIdx*10)
    			retStr += "0";
    	}
    	
    	retStr += String(numToPad);
    	return retStr;
    }
    
    //Called to format the time. timerValue is in seconds and so by dividing by 3600 we obtain hours
    //Floor essentially returns the integer part of our value and discards and fractional component.
    //There are 60 seconds in a minute. By dividing our seconds by 60 we obtain the minutes value, however
    //we need to modulate this value by 60 since there are 60 minutes in an hour and we wish to display hours.
    //The seconds place is just modulated by 60 since we wish to display minutes (and we want to show how many seconds
    //are left before the next minute value).
    function displayTime(){
    	timerDisplay.timerText.text = padZero(Math.floor(timerValue/3600),2) + ":" + 
    	padZero(Math.floor(timerValue/60)%60,2) + ":" + padZero(timerValue%60,2);
    }
    I was then able to generate a swf file from this and get it rendering on a render texture in a quick level I made. If I press the T key I toggle the timer between on and off, and if I press R then I reset the timer.

    [shot]http://www.donaldsmay.com/projects/timerudk.jpg[/shot]

    You can download these files from: http://www.donaldsmay.com/projects/TimerTestRelease.zip

    I made every effort to try and comment the code where appropriate to help aid in understanding and I tried to use self documenting code techniques.

    I am sharing this code/project in the hopes that someone finds it useful/helpful .

    Enjoy!
Working...
X