Jump to content
GreenSock

Fitchett Developments

Is there a way that I can check if a function has been called for a certain amount of intervals then call onComplete?

Recommended Posts

Hey everyone so I know that tweenMax has a onComplete function but I was wondering is there anyway that I can setup a tweenMax or tweenLite function to only run 30 intervals of my stages frames per second then once finished call on the onComplete function?

 

I have a movie clip object that is added to the stage every 0.8 seconds and what i wanted to do was when my character picks up a powerup I wanted to have the movie clip object added to the stage every 0.1 seconds but only for a couple intervals or like say 5 seconds then I wanted to put it back to its original time.

 

Is there anything that the tween engine offers to call on a onComplete function for this?

Link to comment
Share on other sites

The easiest thing to do is just recursively call the same function using delayedCall() like this

 

import  com.greensock.*;

var count:int = 0;
var max:int = 5;

function doSomething() {
 if(count <  max){
   trace("doing something " + count);
  TweenLite.delayedCall(0.5, doSomething)// do something again 0.5 seconds from now
count++;
 } else {
trace("done"); // or call whatever function you want
 }
  
}

//start doing something
doSomething();

Another approach would be using a loop to add function callbacks to a timeline at specified intervals and then putting an onComplete on the timeline... but that would only be necessary if you needed advanced controls like pausing and resuming. 

Link to comment
Share on other sites

Awesome thanks so much Carl Ill give it try today and let you know how it turned out. I hope this is not going against the forum rules but maybe you can help me answer this quick question. I was wondering if replacing ENTER_FRAME events in my Movie Clip objects main class to TweenLite would increase performance? RIght now in the Movie Clip Object classes I have the ENTER_FRAME event moving the Objects across the screen in a linear line with "this.x += 10" etc... Would it increase performance to just remove that and instead replace it with the Tweenlite? I ask because I am publishing the game to Android and the better perfromance the easier on the CPU ya know? I would appreciate any feedback thank you again!

Link to comment
Share on other sites

I don't think you would notice a difference or even be able to measure a difference unless you had thousands of objects moving around.

 

For what its worth, a TweenLite tween will always require a little (insignificant) amount of overhead over an ENTER_FRAME because doing 

 

this.x += 10

 

is really easy.

 

A TweenLite tween needs to record starting and ending values, update values, check to see if ending values are met, perhaps apply easing, check to see if the tween should be overwritten etc.

 

Imagine 2 cars on a hill.

 

Car 1 has no engine, steering wheel, gas tank, or brakes.

Car 2 is a regular car

 

Push them both down the hill. Car 1 will get much better gas mileage, but I think you'd rather be in car 2 (TweenLite).

 

Again, this is just to show that TweenLite has much more going for it, but I really doubt you will notice a performance difference between 1 TweenLite tween and 1 ENTER_FRAME approach.

  • Like 1
Link to comment
Share on other sites

Great explanation. I completely understand. That question had be going all night because ive been working on my game for awhile now and I had so many timer events going on at the same time around 8 or 10 Timers I read a lot and understand that Timers cause lag and can decrease performance on Mobile devices etc.. So I removed them all and instead added TweenLite.delayedCall to all my used to be timer functions and have them loop I hope I did that correct. It seems to be working properly just like a timer. I hope I read enough to understand that by removing all the timers and using Tweenlite delayedCall was a intelligent move.

Link to comment
Share on other sites

Hmm I cant seem to get the function to work correctly on the delayed call. When i trace the trace("doing something " + count); as you provided it brings back 0 and stop there it never goes to the else function. ANy reason why this would happen?

Link to comment
Share on other sites

I just pasted the code I provided into a new FLA just to make sure its ok. No problems at all. Works perfectly.

 

This is the output I get

doing something 0
doing something 1
doing something 2
doing something 3
doing something 4
done
 
Guessing you must have changed something along the way.
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Recently Browsing   0 members

    • No registered users viewing this page.
×