00:00
00:00

AS: Intervals

5,922 Views | 26 Replies
New Topic Respond to this Topic

AS: Intervals 2005-06-30 11:40:36


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

Response to AS: Intervals 2005-06-30 11:42:19


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

Response to AS: Intervals 2005-06-30 11:49:31


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

Response to AS: Intervals 2005-06-30 11:51:56


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

Response to AS: Intervals 2005-06-30 11:56:21


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

Response to AS: Intervals 2005-06-30 12:25:34


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

Response to AS: Intervals 2005-06-30 12:30:16


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.

Response to AS: Intervals 2005-06-30 12:37:43


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

Response to AS: Intervals 2005-06-30 12:47:02


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

Response to AS: Intervals 2005-07-22 08:46:04


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.


Blast Pulser Check out my Twin Stick Shooter with procedural bosses!

Response to AS: Intervals 2006-08-02 09:18:33


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?

Response to AS: Intervals 2006-08-02 09:27:27


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.

Response to AS: Intervals 2006-08-02 09:32:44


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?

Response to AS: Intervals 2006-08-02 11:19:07


intervals scare me


- Matt, Rustyarcade.com

Response to AS: Intervals 2007-03-30 14:49:56


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?


BBS Signature

Response to AS: Intervals 2007-03-30 15:27:11


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);

Response to AS: Intervals 2007-03-30 16:02:20


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


BBS Signature

Response to AS: Intervals 2007-03-30 16:07:27


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.

Response to AS: Intervals 2007-03-30 16:09:03


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


BBS Signature

Response to AS: Intervals 2007-03-30 16:09:53


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.


BBS Signature

Response to AS: Intervals 2007-03-30 16:11:34


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.


BBS Signature

Response to AS: Intervals 2007-03-30 16:12:02


Because you have no clue what goes where?


BBS Signature

Response to AS: Intervals 2007-03-30 16:13:59


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.


BBS Signature

Response to AS: Intervals 2007-03-30 16:23:53


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


BBS Signature

Response to AS: Intervals 2007-03-30 16:40:47


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.


BBS Signature

Response to AS: Intervals 2008-05-18 15:16:54


how would you do this on frame coding


BLEACH ENGINE UPGRADED

If U Can Help Please Pm Me Or Tell Me Here

Bleach Engine 1.0

Response to AS: Intervals 2008-11-27 08:11:00


really good tutorial!

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