PDA

View Full Version : Single Execution setInterval



venkat2cool
01-12-2007, 05:29 PM
Hi,

Is there anyway in Flash to run a command only once after a certain amount of time... precisely the way we use setTimeout in JavaScript?

Currently I have declared a variable and done a clearInterval the first time when the function is fired. But my problem is that there are almost 7-8 such timeouts that I have to do in regular intervals.

So... i wanted to know if there is an efficient method to handle this in ActionScript...

Regards,
Venkat:confused:

BLiZZaRD
01-13-2007, 03:27 PM
There are several ways to do so, you mention after a certain amount of time, though.. are you using a timer of any sort, or just going by Frame rate?

venkat2cool
01-28-2007, 04:44 PM
I want to use a timer. Nothing frame-based. Something like a function should be called after 500 milliseconds and it should be executed only once.

BLiZZaRD
01-28-2007, 05:31 PM
Something like this should be tweakable for you then. I haven't tested it's functionality, but it looks correct to me :D

I do have Flash open if you need help, but I am building Tilesets at the moment.. bitmap data can be so boring....



function getRand(maxNum, minNum) {
return Math.floor(Math.random() * (maxNum - minNum)) + minNum;
}

var startTime = getTimer();
var limitTime = getRand(3, 1) * 1000;
var runTime = true;
_root.onEnterFrame = function() {
if (runTime) {
if (getTimer() - startTime > limitTime) {
//do action
trace("time's up!");
runTime = false;
}
}
};

BLiZZaRD
01-28-2007, 05:41 PM
Alternatley some more options from Morgan:



Yep, this can be done in scripting, Zoranvedek is on the right lines.

There are two easy ways I can think of. To delay by a number of frames:

When you want the delay to start, set a variable to 0. Have a script running once every frame, to increment the variable by 1. Have another part of the script running so that when the variable exceeds 20, to then go on and do something else.
If you're using 20 frames per second, this would pause the movie for one second.

ie.
In the starting script:
status="BeforeWait"

In a script running once per frame in the movie:
if (status=="BeforeWait")
{
// All the other stuff you want to do before the wait
}

if (status=="StartWait")
{
counter=0
status="NowWaiting"
}

if (status=="NowWaiting")
{
counter+=1
if (counter>20)
{
status="FinishedWaiting"
}
}

if (status=="FinishedWaiting")
{
// All the stuff you want to do after the wait is completed
}

On a button when you want the delay to start, add an 'On Button Up' script of:
status="StartWait"

For longer delays, change the line : if (counter>20) to be a bigger number. If your movie doesn't run at 20 frames per second, change this accordingly.


If you need it to be a strict time delay, then the same principle can be used with the time variables, or probably the better one would be the timer() function, which just returns a number rather than having to mess about with hours, minutes, seconds, days, etc.

ie.
In the starting script:
status="BeforeWait"

In a script running once per frame in the movie:
if (status=="BeforeWait")
{
// All the other stuff you want to do before the wait
}

if (status=="StartWait")
{
counter=getTimer()
status="NowWaiting"
}

if (status=="NowWaiting")
{
if (getTimer()>(counter+1000))
{
status="FinishedWaiting"
}
}

if (status=="FinishedWaiting")
{
// All the stuff you want to do after the wait is completed
}

On a button when you want the delay to start, add an 'On Button Up' script of:
status="StartWait"

This would wait 1000 milliseconds. The method you use probably depends on if you want to wait for a number of frames, or for a specific amount of time.

Morgan.