Forum Topic: AS: Intervals

(4,135 views • 26 replies)

This topic is 1 page long.

<< < > >>
None

Inglor

Reply To Post Reply & Quote

Posted at: 6/30/05 11:40 AM

Inglor NEUTRAL LEVEL 17

Sign-Up: 01/26/03

Posts: 10,948

AS: Main
This is a very simple tutorial assuming you know how to use Functions

Did you ever want to time a function to run outside onEnterFrame? every specific amount of time? for example ever second? every 1/20th of a second, every minute?

this is very useful for the following

autosave for games
fast and slow motion, dynamically changing the FPS
Typewriter Effect
and many others...

The concept is quite simple, flash lets you set and remove intervals, intervals are those handlers who trigger every specified amount of time. every interval has an ID, a number that is your reference to that interval.

an interval has a function, a timed (in miliseconds) and an ID

_root.stop;
id = setInterval(function () {
_root.nextFrame();
}, 200);

this for example sets the FPS on _root to 5, nice eh?

the syntax is

idvariable=setInterval (f:Function,interval);

if you want to stop the interval you just type

clearInterval (idvariable);

now let's say I want to trace "lolk" every 5 seconds, all I have to do is

idvariable=setInterval(function(){
trace("hello");
},5000);

I will cover passing parameters to functions set in setInterval later, but it's usually not that importnet

post any questions

one useful link to rule them all


None

Dancing-Thunder

Reply To Post Reply & Quote

Posted at: 6/30/05 11:42 AM

Dancing-Thunder NEUTRAL LEVEL 04

Sign-Up: 04/19/05

Posts: 278

Nice, I might end up using this in the future!


None

T-H

Reply To Post Reply & Quote

Posted at: 6/30/05 11:49 AM

T-H LIGHT LEVEL 39

Sign-Up: 01/07/04

Posts: 4,893

I use Intervals quite a lot, I can't get over how great they would be if you could run them faster than the framerate though...


None

Inglor

Reply To Post Reply & Quote

Posted at: 6/30/05 11:51 AM

Inglor NEUTRAL LEVEL 17

Sign-Up: 01/26/03

Posts: 10,948

of course you can

1)set the global framerate to the max
2)create an interval script that stops the movie from playing and moves it with intervals and nextFrame

there, yourframerate is selected through stage 2 :P


None

Denvish

Reply To Post Reply & Quote

Posted at: 6/30/05 11:56 AM

Denvish DARK LEVEL 46

Sign-Up: 04/25/03

Posts: 16,236

Cool beans. I have one question, though. How the FUCK did you get both the A and the S in the topic title to be capitals?????

Seriously though, being able to customise frame rate is a definitely a great thing. I can see it causing problems with streamed music on the main timeline, but there are a couple of ways to get round that (sound MCs or attachSound).

- - Flash - Music - Images - -

BBS Signature

None

Inglor

Reply To Post Reply & Quote

Posted at: 6/30/05 12:25 PM

Inglor NEUTRAL LEVEL 17

Sign-Up: 01/26/03

Posts: 10,948

At 6/30/05 11:56 AM, Denvish wrote: Cool beans. I have one question, though. How the FUCK did you get both the A and the S in the topic title to be capitals?????

I clicked SHIFT when typing them ? :P isn't SHIFT is teh ownzors?!


None

T-H

Reply To Post Reply & Quote

Posted at: 6/30/05 12:30 PM

T-H LIGHT LEVEL 39

Sign-Up: 01/07/04

Posts: 4,893

At 6/30/05 12:25 PM, Inglor wrote: I clicked SHIFT when typing them ? :P isn't SHIFT is teh ownzors?!

Not for me it isn't :(

You must be magic.


None

Denvish

Reply To Post Reply & Quote

Posted at: 6/30/05 12:37 PM

Denvish DARK LEVEL 46

Sign-Up: 04/25/03

Posts: 16,236

At 6/30/05 12:30 PM, T-H wrote:
At 6/30/05 12:25 PM, Inglor wrote: I clicked SHIFT when typing them ? :P isn't SHIFT is teh ownzors?!
Not for me it isn't :(

You must be magic.

Only partially magic =)
It's a very strange filter they use on topic titles. Sometimes the non-caps business works too well, and sometimes it seems to not bother...

- - Flash - Music - Images - -

BBS Signature

None

Inglor

Reply To Post Reply & Quote

Posted at: 6/30/05 12:47 PM

Inglor NEUTRAL LEVEL 17

Sign-Up: 01/26/03

Posts: 10,948

yea, it's really strange, I put capitols in that thread too... hmmm, so what's different?


None

WarpZone

Reply To Post Reply & Quote

Posted at: 7/22/05 08:46 AM

WarpZone DARK LEVEL 14

Sign-Up: 01/26/05

Posts: 96

Here's some interesting details about setInterval. I discovered these things through trial and error, and I figure I should post them here so other people won't waste time trying to use setInterval for things it can't do.

First of all, you don't want to call setInterval in your main loop. If you do, it will wait however many milliseconds, and then perform the action every single frame after that point. The proper way to use setInterval is to run it once at the begining of your program, and then cancel it later.

Is there any way to test and see if an interval is set or not? That would be useful for starting an interval up again in mid-game.

Secondly, there's no point in trying to use setInterval to trigger a function that you want to pass dynamic values to. I am probabaly using the word "dynamic" incorrectly here. By dynamic, I mean values that get their data from an object, such as _root.mymc._width or Math.random()*1000) Values such as these are set in stone when the interval is set.

In my case, I wanted to do this:

setInterval (spawnExplosion, 1000, (Math.random()*_root.screenWidth),(Math.ra
ndom()*_root.screenHeight), 1, 0, "kaboom");

So that explosions would appear all over the screen in random positions. Well, instead, they all appear in exactly the same place, although it is a different place every time the game is run.

So while setInterval may be useful for some things, but it doesn't work the way I thought it did. If you're using it to trigger an existing function, you can only pass values to the function once when the function is run. Now, if I had generated the random numbers within my spawnExplosion function, this would have worked.

I can see now why you didn't bother showing us how to pass values to a function. It isn't all that useful in most situations. You might as well retrieve the values from within the function you're calling.

Guess I'll be incrementing a variable every frame, the old fashioned way. :P

I hope one or two people find this post useful.


None

NAMKCOR

Reply To Post Reply & Quote

Posted at: 8/2/06 09:18 AM

NAMKCOR DARK LEVEL 08

Sign-Up: 01/30/06

Posts: 164

even after all that explanation, I am still rather confused
as to what you can and cannot do.
it is not possible to have numbers change from within the setInterval loop but
it is possible to do it from a function in the setInterval loop? is that right?


None

XBigTK13X

Reply To Post Reply & Quote

Posted at: 8/2/06 09:27 AM

XBigTK13X LIGHT LEVEL 28

Sign-Up: 08/04/05

Posts: 370

Here's another things to add, I'm pretty sure I didn't see it up there. Sorry if I missed it ^_^. If you use a variable to store an Interval (IE: n00bTimer=setInterval(n00bfunction,1000/fp
s;) and then leave the frame that funciton was created on for any amount of time when you return to that frame the function runs at the speed of the following simple formula

numTimesFuncRun=numFunctionInterval/(numTi
mesLeftFrame+1).

There's not really a forumla that does that...I think. But to fix the error, you have to have a script that used clearInterval(intervalName); before you leave the frame. I've since found it much easier to just contain all my code within 1 frame on the maintimeline to avoid this problem throughout a bunch of MCs.


None

NAMKCOR

Reply To Post Reply & Quote

Posted at: 8/2/06 09:32 AM

NAMKCOR DARK LEVEL 08

Sign-Up: 01/30/06

Posts: 164

so when you leave the frame it is on, it continues to run
unless you did a clearInterval in the frame it is on right?


None

Rustygames

Reply To Post Reply & Quote

Posted at: 8/2/06 11:19 AM

Rustygames LIGHT LEVEL 18

Sign-Up: 05/07/05

Posts: 6,662

intervals scare me

- Matt, Rustyarcade.com


None

crushy

Reply To Post Reply & Quote

Posted at: 3/30/07 02:49 PM

crushy LIGHT LEVEL 15

Sign-Up: 09/17/05

Posts: 1,842

I need help, everytime I want to run my code:

onClipEvent (enterFrame) {
idvariable=setInterval(function(){
this._x=_root.player._x;
this._y=_root.player._y;
}
},5000);

I get this:

**Error** Scene=Scene 1, layer=gameplay, frame=5:Line 6: ')' or ',' expected
},5000);

Total ActionScript Errors: 1 Reported Errors: 1

Where are they expected?

Learning AS3 :D
Help me fix Flash :(

BBS Signature

None

BleeBlap

Reply To Post Reply & Quote

Posted at: 3/30/07 03:27 PM

BleeBlap LIGHT LEVEL 24

Sign-Up: 03/08/05

Posts: 944

At 3/30/07 02:49 PM, crushy wrote: **Error** Scene=Scene 1, layer=gameplay, frame=5:Line 6: ')' or ',' expected
},5000);

Total ActionScript Errors: 1 Reported Errors: 1

Where are they expected?

It is expecting you to either close the setInterval call or add a comma to seperate parameters, instead it finds an extra }. The correct code is:

onClipEvent (enterFrame) {
idvariable=setInterval(function(){
this._x=_root.player._x;
this._y=_root.player._y;
},5000);


None

crushy

Reply To Post Reply & Quote

Posted at: 3/30/07 04:02 PM

crushy LIGHT LEVEL 15

Sign-Up: 09/17/05

Posts: 1,842

At 3/30/07 03:27 PM, BleeBlap wrote:
At 3/30/07 02:49 PM, crushy wrote: **Error** Scene=Scene 1, layer=gameplay, frame=5:Line 6: ')' or ',' expected
},5000);

Total ActionScript Errors: 1 Reported Errors: 1

Where are they expected?
It is expecting you to either close the setInterval call or add a comma to seperate parameters, instead it finds an extra }. The correct code is:

onClipEvent (enterFrame) {
idvariable=setInterval(function(){
this._x=_root.player._x;
this._y=_root.player._y;
},5000);

Damn, when I use that code it says:

**Error** Scene=Scene 1, layer=gameplay, frame=5:Line 1: Statement block must be terminated by '}'
onClipEvent (enterFrame) {

**Error** Scene=Scene 1, layer=gameplay, frame=5:Line 6: Syntax error.

Total ActionScript Errors: 2 Reported Errors: 2

Learning AS3 :D
Help me fix Flash :(

BBS Signature

None

BleeBlap

Reply To Post Reply & Quote

Posted at: 3/30/07 04:07 PM

BleeBlap LIGHT LEVEL 24

Sign-Up: 03/08/05

Posts: 944

At 3/30/07 04:02 PM, crushy wrote:
At 3/30/07 03:27 PM, BleeBlap wrote:
At 3/30/07 02:49 PM, crushy wrote: **Error** Scene=Scene 1, layer=gameplay, frame=5:Line 6: ')' or ',' expected
},5000);

Total ActionScript Errors: 1 Reported Errors: 1

Where are they expected?
It is expecting you to either close the setInterval call or add a comma to seperate parameters, instead it finds an extra }. The correct code is:

onClipEvent (enterFrame) {
idvariable=setInterval(function(){
this._x=_root.player._x;
this._y=_root.player._y;
},5000);
Damn, when I use that code it says:

**Error** Scene=Scene 1, layer=gameplay, frame=5:Line 1: Statement block must be terminated by '}'
onClipEvent (enterFrame) {

**Error** Scene=Scene 1, layer=gameplay, frame=5:Line 6: Syntax error.

Total ActionScript Errors: 2 Reported Errors: 2

After actually reading your code I realized you have several problems. First you are creating an interval every frame. If you want that to be at the same x,y every frame you don't need an interval, just put the this._x = player._x bit in the onEnterFrame. If you do want it on an interval (which will create a lag effect) then don't put it in the onEnterFrame. You only need to create the interval once. Finally, inside the scope of the function in the interval, this won't refer to what you want it to. To do that you'll have to pass a reference to the object you want moved to the interval function.

Oh yeah, and to fix the syntax error add an extra '}' at the end to close the clip event handler.


None

GustTheASGuy

Reply To Post Reply & Quote

Posted at: 3/30/07 04:09 PM

GustTheASGuy LIGHT LEVEL 08

Sign-Up: 11/02/05

Posts: 11,358

You are reading, thus you have eyes. The handler is missing a bracket.

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


None

crushy

Reply To Post Reply & Quote

Posted at: 3/30/07 04:09 PM

crushy LIGHT LEVEL 15

Sign-Up: 09/17/05

Posts: 1,842

Its there becouse the code is inside a vcam and player is a ball so its following the bounces.ect but I want it to be slightly delayed.

Learning AS3 :D
Help me fix Flash :(

BBS Signature

None

crushy

Reply To Post Reply & Quote

Posted at: 3/30/07 04:11 PM

crushy LIGHT LEVEL 15

Sign-Up: 09/17/05

Posts: 1,842

At 3/30/07 04:09 PM, GustTheASGuy wrote: You are reading, thus you have eyes. The handler is missing a bracket.

When I add a brack the first problem rearrises.

Learning AS3 :D
Help me fix Flash :(

BBS Signature

None

GustTheASGuy

Reply To Post Reply & Quote

Posted at: 3/30/07 04:12 PM

GustTheASGuy LIGHT LEVEL 08

Sign-Up: 11/02/05

Posts: 11,358

Because you have no clue what goes where?

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


None

crushy

Reply To Post Reply & Quote

Posted at: 3/30/07 04:13 PM

crushy LIGHT LEVEL 15

Sign-Up: 09/17/05

Posts: 1,842

At 3/30/07 04:12 PM, GustTheASGuy wrote: Because you have no clue what goes where?

When I add the bracketat the end, no errors appear but the vcam doesnt do its job and its back to how I started.

Learning AS3 :D
Help me fix Flash :(

BBS Signature

None

GustTheASGuy

Reply To Post Reply & Quote

Posted at: 3/30/07 04:23 PM

GustTheASGuy LIGHT LEVEL 08

Sign-Up: 11/02/05

Posts: 11,358

Your point naturally being that you yourself are again hoping to get away without stressing your little mind with logic.

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


None

crushy

Reply To Post Reply & Quote

Posted at: 3/30/07 04:40 PM

crushy LIGHT LEVEL 15

Sign-Up: 09/17/05

Posts: 1,842

At 3/30/07 04:23 PM, GustTheASGuy wrote: Your point naturally being that you yourself are again hoping to get away without stressing your little mind with logic.

sorry? I'm only trying to learn intervals.

Learning AS3 :D
Help me fix Flash :(

BBS Signature

None

TrillingBird

Reply To Post Reply & Quote

Posted at: 5/18/08 03:16 PM

TrillingBird LIGHT LEVEL 03

Sign-Up: 04/11/08

Posts: 291


None

nick9001

Reply To Post Reply & Quote

Posted at: 11/27/08 08:11 AM

nick9001 LIGHT LEVEL 07

Sign-Up: 05/05/08

Posts: 75

really good tutorial!

ty but is there some way every certain amount of time i can insert a movie clip?


All times are Eastern Standard Time (GMT -5) | Current Time: 12:28 AM

<< 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!