00:00
00:00
Newgrounds Background Image Theme

mariobros22 just joined the crew!

We need you on the team, too.

Support Newgrounds and get tons of perks for just $2.99!

Create a Free Account and then..

Become a Supporter!

Timed functions -AS3-

564 Views | 6 Replies
New Topic Respond to this Topic

Timed functions -AS3- 2015-01-19 19:42:28


When it comes to functions that are only meant to run for a short time and then stop, is there a way for that same function to create its own timer and then use it to time the function's run? This probably isn't a thing, but what about creating a new "instance" of a function when you call it?

For the longest time, I've always had to create an int timer in the class variables and then iterate that timer from the function. When the timer reaches a certain amount, I'll usually do something to end the function and reset the timer. Like so:

//class variables
  var counter:int = 0;
  var condition:Boolean = false;

//function tied to the ENTER_FRAME
  if(condition == true)[
    timedFunction();
  }

//and the timed function
  function timedFunction(){
    counter++;
    if(counter >= 30){
      condition = false;
      counter = 0;
    }
  }

Normally, I don't mind keeping the timer independent of the function, but I'm trying to turn such functions into static functions that I can keep in that Utility class I talked about before. The function I'm working on now is one that affects the color of an object, but I want to be able to reuse it through the whole project without having to paste the function and variable into each class.

I know I can paste the variable into the Utility class and use it that way, but that means that each object using this function will be on the exact same time. if I wanted something to occur at a specific timing moment, it wouldn't happen equally on each object.

So, I'm not sure. All I can think of is creating a new timer or somehow using a new instance of a function each time it's called. If there's another way, I'm all ears.

Response to Timed functions -AS3- 2015-01-19 21:04:45


At 1/19/15 07:42 PM, Barzona wrote: For the longest time, I've always had to create an int timer in the class variables and then iterate that timer from the function. When the timer reaches a certain amount, I'll usually do something to end the function and reset the timer. Like so:

You're counting frame ticks there, not time. Technically speaking you are counting (1/x)*1000 milliseconds, approximately, with each call of that function, where x is your FPS. This is because that your onEnterFrame handler is called once for each frame tick, x number of times per second where x is your FPS. In other words, if you are using 60FPS then your onEnterFrame handler will be called 60 times per second.

To more accurately track time you should use the getTimer function. Each call will return the number of milliseconds since your SWF application was started, so you simply need to store the return value of it before calling a function and then afterward and then calculate the difference, which will be the number of milliseconds it took for your function to execute.

Response to Timed functions -AS3- 2015-01-19 22:04:21


At 1/19/15 09:04 PM, Diki wrote: You're counting frame ticks there, not time. Technically speaking you are counting (1/x)*1000 milliseconds, approximately, with each call of that function, where x is your FPS. This is because that your onEnterFrame handler is called once for each frame tick, x number of times per second where x is your FPS. In other words, if you are using 60FPS then your onEnterFrame handler will be called 60 times per second.

Well, I use the term "timer" loosely. The program does run at 60 fps, so when I ever do need to time something at real time, I do just base it off that. For now, I'm just talking about running a function for around 20 frames and stopping it.

To more accurately track time you should use the getTimer function. Each call will return the number of milliseconds since your SWF application was started, so you simply need to store the return value of it before calling a function and then afterward and then calculate the difference, which will be the number of milliseconds it took for your function to execute.

I get that, and it sounds like a good solution to half the problem, but what about being able to use the same timed function on multiple objects at once (but the function starts at different times for each object)? With only this solution, if one object was using the function and another object started using it a few frames later, they'd both be forced to stop it at the same time since the same function would be running for both.

When the function was written across different classes, each instance of the class had access to its own copy of the function. Now that I'm trying to keep only one copy of the function in this Utility class, I'm trying to see if there's a way to call the function as a new version of it, so that each object could be affected uniquely.

Oh, I think I got it..

Seems all I had to do was create an instance of the utility class, remove the static from the function, and then call it by its reference. All i could come up with.

Response to Timed functions -AS3- 2015-01-19 22:09:34


At 1/19/15 10:04 PM, Barzona wrote: Seems all I had to do was create an instance of the utility class, remove the static from the function, and then call it by its reference. All i could come up with.

Make a new Timer object each time or store the counter and callback function in an array or something and update each one.

Response to Timed functions -AS3- 2015-01-19 22:33:07


At 1/19/15 10:04 PM, Barzona wrote: Well, I use the term "timer" loosely. The program does run at 60 fps, so when I ever do need to time something at real time, I do just base it off that. For now, I'm just talking about running a function for around 20 frames and stopping it.

What exactly are you trying to do with these timers and functions?

Response to Timed functions -AS3- 2015-01-19 23:03:33


At 1/19/15 10:33 PM, Diki wrote: What exactly are you trying to do with these timers and functions?

This function makes an object flash different colors which is just a color transform that eases into one intensity, then eases back down to normal and it just repeats that. It uses the timer to control when the color changes. Like, when the timer is within a certain range, it's increasing the color, when it's within another range, it reduces the color.

At 1/19/15 10:09 PM, MSGhero wrote: Make a new Timer object each time or store the counter and callback function in an array or something and update each one.

I've loaded functions from arrays before, but I don't understand how doing so would make a new instance of it..

It's cool, really. I don't mind just creating an instance of the class and calling the function that way. It does what I've been wanting.

Response to Timed functions -AS3- 2015-01-20 00:15:08


At 1/19/15 11:03 PM, Barzona wrote: This function makes an object flash different colors which is just a color transform that eases into one intensity, then eases back down to normal and it just repeats that. It uses the timer to control when the color changes. Like, when the timer is within a certain range, it's increasing the color, when it's within another range, it reduces the color.

Sounds like you're performing a tween, in which case you should use a tweening library, such as TweenLite.