Forum Topic: AS3: Counter

(946 views • 8 replies)

This topic is 1 page long.

<< < > >>
Thinking

LilFugitive

Reply To Post Reply & Quote

Posted at: 11/10/07 06:46 AM

LilFugitive EVIL LEVEL 16

Sign-Up: 06/01/06

Posts: 568

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

Ruining your life since 2006

BBS Signature

None

GustTheASGuy

Reply To Post Reply & Quote

Posted at: 11/10/07 07:36 AM

GustTheASGuy LIGHT LEVEL 08

Sign-Up: 11/02/05

Posts: 11,370

'getTimer' is not a global function, it's in flash.utils. -_-

#ngprogramming at irc.freenode.net
haXe | Keel imperative | Spyro! | Thru you


None

GustTheASGuy

Reply To Post Reply & Quote

Posted at: 11/10/07 07:38 AM

GustTheASGuy LIGHT LEVEL 08

Sign-Up: 11/02/05

Posts: 11,370

Also your code is not indented. Give it more thought whydoncha.

#ngprogramming at irc.freenode.net
haXe | Keel imperative | Spyro! | Thru you


None

LilFugitive

Reply To Post Reply & Quote

Posted at: 11/10/07 09:50 AM

LilFugitive EVIL LEVEL 16

Sign-Up: 06/01/06

Posts: 568

At 11/10/07 07:38 AM, GustTheASGuy wrote: Also your code is not indented. Give it more thought whydoncha.

I thought that it would auto-format, sorry

Ruining your life since 2006

BBS Signature

None

archeris

Reply To Post Reply & Quote

Posted at: 7/21/08 08:37 PM

archeris EVIL LEVEL 19

Sign-Up: 04/01/06

Posts: 65

At 11/10/07 09:50 AM, LilFugitive wrote:
At 11/10/07 07:38 AM, GustTheASGuy wrote: Also your code is not indented. Give it more thought whydoncha.
I thought that it would auto-format, sorry

Hey, your the same level and aura as I am :D


None

gorman2001

Reply To Post Reply & Quote

Posted at: 7/21/08 08:41 PM

gorman2001 NEUTRAL LEVEL 14

Sign-Up: 08/18/02

Posts: 1,935

nice little snippet.

the best thing would be to make it into a class, so we could easily instanciate counters...


None

odnarble

Reply To Post Reply & Quote

Posted at: 1/17/09 09:22 PM

odnarble DARK LEVEL 05

Sign-Up: 01/03/09

Posts: 95

This works great. Thanks for this tut. The AS3:main idea is genius.

If at first you don't succeed, redefine success.

BBS Signature

None

Davidzx

Reply To Post Reply & Quote

Posted at: 1/17/09 09:52 PM

Davidzx LIGHT LEVEL 27

Sign-Up: 01/15/07

Posts: 4,255

normally I make up my own codes and get creative, if I need to.


None

Nano256

Reply To Post Reply & Quote

Posted at: 1/18/09 03:57 AM

Nano256 DARK LEVEL 13

Sign-Up: 02/12/05

Posts: 1,474

At 11/10/07 07:36 AM, GustTheASGuy wrote: 'getTimer' is not a global function, it's in flash.utils. -_-

Not unless it's code on the timeline, in which case the utils class does not need to be explicitly referenced or imported when using it.

Move on to ActionScript 3.0 already!
The third post below this one is a lie.

BBS Signature

All times are Eastern Standard Time (GMT -5) | Current Time: 11:44 PM

<< Back

This topic is 1 page long.

<< < > >>
You need a Grounds Gold Account to post on the NG BBS! If you don't have one, click here to sign up now! It's fast, free, and easy — and opens up tons of great NG features!