AS3: Main
Notes:
If you're lazy and just want to copy the code you need, scroll to the bottom.
Also at some steps the code won't work as desired, but that's because I'll slowly change the code and add part by part to it, so read it patiently to really understand it
What am I going to explain?
I'm going to explain how to make a countdown timer that shows how long the person has the flash opened. With this code it doesn't matter what the framerate is.
Before I start with coding, what must I do?
Draw a text box, select at properties that the text box is a Dynamic Text Box. Give the text box the instance name time.
Lest start with the code!
First when the flash starts we want flash to find out how late it is. So we make a int variable and call it startingTime, then the value of startingTime is the time in milli seconds!
var startingTime:int = getTimer ();
Now we want Flash to calculate the difference between the starting time and the current time. We'll use an ENTER_FRAME listener, called calculateDifference
addEventListener(Event.ENTER_FRAME, calculateDifference);
Afterwards we'll add the function of calculateDifference. We want to get the time every frame and remove the starting time of the flash. That's how we'll get the time difference. We'll do this with a int variable called timePassed.
function calculateDifference (event:Event){
var timePassed:int = getTimer() - startingTime;
}
But we want to display it in the Dynamic Text Box called time, so we'll change the calculateDifference function in to this:
function calculateDifference (event:Event){
var timePassed:int = getTimer() - startingTime;
time.text = timePassed;
}
If you would now test it, it would be crap. Because you'll see just a bunch of numbers, those are milliseconds, but we want it to be more organized. We want to see something like:
You've been playing for: 1:13, what would mean that he's been playing for 1 minute and 13 seconds.
So we'll make two new variables: seconds and minutes. I don't really have to explain what their purpose is. We'll change the calculateDifference function to this:
function calculateDifference (event:Event){
var timePassed:int = getTimer() - startingTime;
var seconds:int = Math.floor (timePassed / 1000);
var minutes:int = Math.floor (seconds / 60);
time.text = timePassed;
}
Afterwards we'll change the time.text to display the minutes and seconds and a small "You've been playing for: " text before the numbers.
function calculateDifference (event:Event){
var timePassed:int = getTimer() - startingTime;
var seconds:int = Math.floor (timePassed / 1000);
var minutes:int = Math.floor (seconds / 60);
time.text = String ("You've been playing for: " + minutes ":" + seconds);
}
You'll see that if you wait one minute and one second you'll see:
You've been playing for: 1:61 instead of You've been playing for: 1:01
So we'll flash that if the minute variable 1 is that we need to subtract 60 from seconds variable. With 2 minutes subtract 120 seconds etc. So now are calculateDifference function will look like this:
function calculateDifference (event:Event){
var timePassed:int = getTimer() - startingTime;
var seconds:int = Math.floor (timePassed / 1000);
var minutes:int = Math.floor (seconds / 60);
seconds -= minutes * 60;
time.text = String ("You've been playing for: " + minutes ":" + seconds);
}
Our last problem is that when the counter is running for 3 seconds you will see:
You've been playing for: 0:3 instead of You've been playing for: 0:03
So we're going to say to flash that if seconds is smaller than 10 we'll add a 0 in front of the seconds. Otherwise if seconds is equal or bigger than 10 there won't be a Our calculateDifference function will look like this:
function calculateDifference (event:Event){
var timePassed:int = getTimer() - startingTime;
var seconds:int = Math.floor (timePassed / 1000);
var minutes:int = Math.floor (seconds / 60);
seconds -= minutes * 60;
if (seconds < 10){
time.text = String ("You've been playing for: " + minutes +":0" + seconds);
}
else if (seconds >= 10){
time.text = String ("You've been playing for: " + minutes + ":" + seconds);
}
}
So this is the complete code:
var startingTime:int = getTimer ();
addEventListener(Event.ENTER_FRAME, calculateDifference);
function calculateDifference (event:Event){
var timePassed:int = getTimer() - startingTime;
var seconds:int = Math.floor (timePassed / 1000);
var minutes:int = Math.floor (seconds / 60);
seconds -= minutes * 60;
if (seconds < 10){
time.text = String ("You've been playing for: " + minutes +":0" + seconds);
}
else if (seconds >= 10){
time.text = String ("You've been playing for: " + minutes + ":" + seconds);
}
}
I hope you guys found this tutorial helpful =D
Do you have any comments on this tutorial or problems PM me or leave a comment