[FMX] What is the most effecient way to do a timer?

I’m trying to figure out the most effecient way to do a timer.

Basically, I have the number of seconds for when the action should be done next.

Do I just loop 10 frames or so and check the getTimer or is there a better way to do it?

Actually, is it accurate to just have a movie that is running at 10fps, loops 10 frames, and has a time=time+1 script in it?

you should use setInterval. Its based on time and not frames so you could have your frame rate at anything and a setInterval timer wouldnt be affected. Plus, with setInterval, the timer operation would only happen when you need it to. In the case of a second-based timer, only once every second and not once every frame.

display.text = myTimer = 0;
showTimer = function(){
display.text = ++myTimer;
}
timerInterval = setInterval(showtimer,1000);

(btw ++myTimer is the same as saying myTimer = myTimer +1)

So here’s the thing. It’s more of a countdown. When the movie first runs, it gets the number of seconds and puts it into trackRemain.

I then show a countdown. When it gets to zero it needs to get a new number of seconds for trackRemain.

So I’m not quite sure how to use setInterval as a timer to compare it to trackRemain.

Ok - I think I get it.

I’m using the setInterval to subtract a second every second. :slight_smile:

Is there a way to make this countdown from a specific number? For instance how would you make it countdown from 30 minutes? And is there a way to format the countdown so it looks like a clock (ie 30:00). thanks

David

Bummer. Every time the setInterval gets reset, it goes wacky! I decided to just make a movie that is 10 frames long that loops, and make the fps 10. It’s accurate enough. :slight_smile:

David,
I just dealt with that issue. I’m sure there are several ways to do it, but here is something basic I wrote on my own. I’m new at FMX actionscripts so the code isn’t all that dynamic yet. Read through this explanation first and then look at the code.

trackRemain is the amount of time that my countdown starts with (in seconds). Set this value somewhere outside of your loop.

[font=courier]trackRemain = 1800[/font]

MinuteRemain converts the seconds into minutes (divides by 60) and then takes the integer of that result (rounds it to whole number). This will give you the number to put in the “00:” of your display.

SecondRemain takes the total number of seconds (from trackRemain) and subtracts MinuteRemain*60 from it. This will give you the number to put in the “:00” of your display.

If you want your time to display with a leading zero (05:04 instead of 5:4), we need to check the length of your minutes and seconds. The code basically says, “if MinuteRemain is only one digit, then add a “0” to the front of it”.

I have a dynamic text field titled AutoDisplay that will display the results of the conversion and adds a “:” between them.
[font=Courier]
// Converts seconds to minutes and seconds
MinuteRemain = int(trackRemain/60);
SecondRemain = ((trackRemain) - (MinuteRemain*60));
// Adds a leading zero to Minutes
if (length(MinuteRemain) == 1) {
MinuteRemain = “0”+MinuteRemain;
}
// Adds a leading zero to Seconds
if (length(SecondRemain) == 1) {
SecondRemain = “0”+SecondRemain;
}
AutoDisplay = MinuteRemain + “:” + SecondRemain;
[/font]
At the end of my one second loop, I subtract a second from trackRemain and check to see it equals 0. If it does, I got to a frame outside of the loop called “countdownended”. If it does not equal 0, it goes to the beginning of the loop.
[font=Courier]
trackRemain = trackRemain-1;
if (trackRemain = 0) {
trackRemain = 0;
autoDisplay = “00:00”;
gotoAndPlay(countdownended);
}
gotoAndPlay(“startloop”);[/font]

Does that make sense? :slight_smile:

I finally got it to work. Here’s the final code. Thanks a million!

David

stop();
trackRemain = 1800;
function timercountdown(){
MinuteRemain = int(trackRemain/60);
SecondRemain = ((trackRemain) - (MinuteRemain*60));
if (length(MinuteRemain) == 1) {
MinuteRemain = “0”+MinuteRemain;
}
if (length(SecondRemain) == 1) {
SecondRemain = “0”+SecondRemain;
}
AutoDisplay = MinuteRemain + “:” + SecondRemain;
if (trackRemain == “0”) {
trackRemain = 0;
autoDisplay = “00:00”;
gotoAndPlay(“end”);
}
trackRemain = trackRemain - 1;
}
setInterval(timercountdown, 1000);

Cool!

For some reason, setInterval was giving me a problem after my countdown reset. After it ran the first countdown with no problem, the second countdown would almost double in time, and the numbers would jump around.

If you wouldn’t mind, could you set your timer to 20 seconds and see if you get the same problem when it resets?

I understand what you mean by it jumping around weirdly if you rerun the script. I don’t ever have to rerun the script though. Once the timer hits zero the movie ends. How are you using your timer? If you have to jump around in a movie, place the script on a seperate level and extend the frames the entire length of the movie.

I’m getting my trackRemain seconds from an XML call. So the countdown is different each time it resets.

I’m writing a Flash based playlist window for my webcast. It displays info about the currently playing track.

The method I’m using of just looping one second worth of frames is working for me. I’ll post the completed project when it’s finished.

So, does anyone know why setInterval goes screwy?

Well… There is an easier and more efficient way than anything you guys said… Albeit… It is all about the frames per second… But you have to set that anyways!



framesPerSecond = 30;
countdownTimer = 0; 

// These two variables sets up a good system... Have
// this in your main onLoad thing.

// This next part have thrown in your onEnterFrame 
// commands.. 

if(countdownTimer > 0){countdownTimer--;}

// This will be thrown wherever you set up the 
// number of seconds you have in a statement
// or whenever you wanna make your timer start

countdownTimer = numberofSeconds * framesPerSecond;


Peace

The majority of our code has to do with parsing the time in to the format “00:00”.

But we have several good ways to do the timer now. :slight_smile: