Be a Supporter!
Inglor
Inglor
  • Member since: Jan. 26, 2003
  • Offline.
Forum Stats
Member
Level 17
Blank Slate
AS: Intervals Jun. 30th, 2005 @ 11:40 AM Reply

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

Dancing-Thunder
Dancing-Thunder
  • Member since: Apr. 19, 2005
  • Offline.
Forum Stats
Member
Level 04
Blank Slate
Response to AS: Intervals Jun. 30th, 2005 @ 11:42 AM Reply

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

T-H
T-H
  • Member since: Jan. 7, 2004
  • Offline.
Forum Stats
Member
Level 40
Blank Slate
Response to AS: Intervals Jun. 30th, 2005 @ 11:49 AM Reply

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

Inglor
Inglor
  • Member since: Jan. 26, 2003
  • Offline.
Forum Stats
Member
Level 17
Blank Slate
Response to AS: Intervals Jun. 30th, 2005 @ 11:51 AM Reply

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

Denvish
Denvish
  • Member since: Apr. 25, 2003
  • Offline.
Forum Stats
Member
Level 46
Blank Slate
Response to AS: Intervals Jun. 30th, 2005 @ 11:56 AM Reply

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
Inglor
Inglor
  • Member since: Jan. 26, 2003
  • Offline.
Forum Stats
Member
Level 17
Blank Slate
Response to AS: Intervals Jun. 30th, 2005 @ 12:25 PM Reply

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

T-H
T-H
  • Member since: Jan. 7, 2004
  • Offline.
Forum Stats
Member
Level 40
Blank Slate
Response to AS: Intervals Jun. 30th, 2005 @ 12:30 PM Reply

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.

Denvish
Denvish
  • Member since: Apr. 25, 2003
  • Offline.
Forum Stats
Member
Level 46
Blank Slate
Response to AS: Intervals Jun. 30th, 2005 @ 12:37 PM Reply

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
Inglor
Inglor
  • Member since: Jan. 26, 2003
  • Offline.
Forum Stats
Member
Level 17
Blank Slate
Response to AS: Intervals Jun. 30th, 2005 @ 12:47 PM Reply

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

WarpZone
WarpZone
  • Member since: Jan. 26, 2005
  • Offline.
Forum Stats
Member
Level 14
Blank Slate
Response to AS: Intervals Jul. 22nd, 2005 @ 08:46 AM Reply

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.

NAMKCOR
NAMKCOR
  • Member since: Jan. 30, 2006
  • Offline.
Forum Stats
Member
Level 08
Blank Slate
Response to AS: Intervals Aug. 2nd, 2006 @ 09:18 AM Reply

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?

XBigTK13X
XBigTK13X
  • Member since: Aug. 4, 2005
  • Offline.
Forum Stats
Member
Level 28
Blank Slate
Response to AS: Intervals Aug. 2nd, 2006 @ 09:27 AM Reply

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.

NAMKCOR
NAMKCOR
  • Member since: Jan. 30, 2006
  • Offline.
Forum Stats
Member
Level 08
Blank Slate
Response to AS: Intervals Aug. 2nd, 2006 @ 09:32 AM Reply

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?

Rustygames
Rustygames
  • Member since: May. 7, 2005
  • Offline.
Forum Stats
Member
Level 19
Programmer
Response to AS: Intervals Aug. 2nd, 2006 @ 11:19 AM Reply

intervals scare me


- Matt, Rustyarcade.com

crushy
crushy
  • Member since: Sep. 17, 2005
  • Offline.
Forum Stats
Member
Level 15
Blank Slate
Response to AS: Intervals Mar. 30th, 2007 @ 02:49 PM Reply

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
BleeBlap
BleeBlap
  • Member since: Mar. 8, 2005
  • Offline.
Forum Stats
Member
Level 24
Blank Slate
Response to AS: Intervals Mar. 30th, 2007 @ 03:27 PM Reply

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

crushy
crushy
  • Member since: Sep. 17, 2005
  • Offline.
Forum Stats
Member
Level 15
Blank Slate
Response to AS: Intervals Mar. 30th, 2007 @ 04:02 PM Reply

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
BleeBlap
BleeBlap
  • Member since: Mar. 8, 2005
  • Offline.
Forum Stats
Member
Level 24
Blank Slate
Response to AS: Intervals Mar. 30th, 2007 @ 04:07 PM Reply

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.

GustTheASGuy
GustTheASGuy
  • Member since: Nov. 2, 2005
  • Offline.
Forum Stats
Member
Level 08
Blank Slate
Response to AS: Intervals Mar. 30th, 2007 @ 04:09 PM Reply

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


BBS Signature
crushy
crushy
  • Member since: Sep. 17, 2005
  • Offline.
Forum Stats
Member
Level 15
Blank Slate
Response to AS: Intervals Mar. 30th, 2007 @ 04:09 PM Reply

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
crushy
crushy
  • Member since: Sep. 17, 2005
  • Offline.
Forum Stats
Member
Level 15
Blank Slate
Response to AS: Intervals Mar. 30th, 2007 @ 04:11 PM Reply

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
GustTheASGuy
GustTheASGuy
  • Member since: Nov. 2, 2005
  • Offline.
Forum Stats
Member
Level 08
Blank Slate
Response to AS: Intervals Mar. 30th, 2007 @ 04:12 PM Reply

Because you have no clue what goes where?


BBS Signature
crushy
crushy
  • Member since: Sep. 17, 2005
  • Offline.
Forum Stats
Member
Level 15
Blank Slate
Response to AS: Intervals Mar. 30th, 2007 @ 04:13 PM Reply

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
GustTheASGuy
GustTheASGuy
  • Member since: Nov. 2, 2005
  • Offline.
Forum Stats
Member
Level 08
Blank Slate
Response to AS: Intervals Mar. 30th, 2007 @ 04:23 PM Reply

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


BBS Signature
crushy
crushy
  • Member since: Sep. 17, 2005
  • Offline.
Forum Stats
Member
Level 15
Blank Slate
Response to AS: Intervals Mar. 30th, 2007 @ 04:40 PM Reply

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
TrillingBird
TrillingBird
  • Member since: Apr. 11, 2008
  • Offline.
Forum Stats
Member
Level 03
Blank Slate
Response to AS: Intervals May. 18th, 2008 @ 03:16 PM Reply

how would you do this on frame coding

nick9001
nick9001
  • Member since: May. 5, 2008
  • Offline.
Forum Stats
Member
Level 08
Blank Slate
Response to AS: Intervals Nov. 27th, 2008 @ 08:11 AM Reply

really good tutorial!

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