Tweener Tips

A few lesser known tips for Tweener.

I've been using for almost an year now, and I thought I'd share a few quick and useful tips.

Delayed Function call, or a Poor man's timer

AS3 has the Timer class which is handy, but sometimes you just need a quick "call this function in x seconds". No need to instantiate an object, keep its reference, add event listeners, a function and clear up the timer. This is so simple (and great):

1
Tweener.addTween(this, {time:0.3, onComplete: myFunction});
Or, using an anonymous function:
1
2
3
4
Tweener.addTween(this, {time:0.3, onComplete: function():void{
        // do something here!
        trace("hello");
});
Note that you don't have to tween any property at all for this to work. Just specify any object as the target, the time in seconds you wish to wait until the function is called and which function to call.

Using "base" as a template for Tweens

Some animations / visual effects are used many times over in the same project. Using the base property you can create a "template" of a tween and specialize it later (like adding an onComplete callback for some runs of the effect). Suppose you have a recurring tween that scale items and does a fade. You can say:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
// creates a "template" to be used more than once:
var scaleFadeIn : Object = {
        alpha:1,
        _scale:1,
        time:0.5,
        transition: "linear"
}
// later in you code you can say:
Tweener.addTween(myMovieClip, {base:scaleFadeIn});
// or you can "enhance it", for example with an onComplete callback:
Tweener.addTween(myMovieClip, {base:scaleFadeIn, onComplete: callHome});

The great thing about this is easier maintenance. If you later on decide that you want to try another transition or time, you only change the template, the "base" for those Tweens.

Note that base is very flexible, as they can be nested:

1
2
3
4
var scaleAndColorFadeIn : Object = {
        base: scaleFadeIn,
        _color: 0xFF0000
}
In this case, you "add" all settings from the scaleFadeIn and the scaleAndColorFadeIn. More on the base property documentation page.

Using setTimeScale to speed up testing

This is a real life saver. Tweener uses a sort of internal clock, a value by which all time operations are measured, called timeScale. Sometimes you are coding some section of the website, but to get there you have to see the loading animations, menu transitions and so on. Because you are seeing this over and over again, those precious seconds until you get to the part that really interests you is very boring, so you can "fast forward" them. At the beginning of your website you can just say:

1
2
// this will run everything 3 times as fast
Tweener.timeScale(3);
Once you've reached the section / part of the website that you are actually working on, you can just set the speed back to normal:
1
2
// this will set the speed back to normal
Tweener.setTimeScale( 1);
Another use for this is when you want to see your animations in "slow motion". Setting time scale will allow the rhythm to be the same, you're just slowing down the tempo, maybe you are interested in looking how a blur really looks or if some items are overlapping.

getting a third opinion

1.
Gabriel Laet says at

Awesome! The third one is the best. I'll definitely start using this.

2.
Og2t says at

Hi Arthur!

Thanks for that! I think there's a mistake though, i.e. setTimeScale(0.5) will slow down the animation twice and setTimeScale(2) will speed it up two times.

For Delayed Function call I often use addCaller method instead, that allows to call the function more than one time, ie.

Tweener.addCaller(this, {onUpdate:myFunction, delay: 2, count: 1});

Cheers!

3.
francisco arenas says at

the base for templates, is the most interesting and useful of tweener's tools, thanks for sharing!!

4.
Ryan says at

timeScale() & setTimeScale() for the score. GOALLLL!!!!! Might be good for replays and bullet time like game effects. I discovered this a few months back and it is very fun. Maybe should post some demos, damn you time!

Thanks Arthur

5.
Arthur Debert says at

: oops! You are correct, setting the time scale to 0.5 will run twice as slow, but I can't find a reference to setTimeScale(0.5) ?! Yup, addCaller is great, it's just that sometimes all you want is one execution. For multiple calls with easing addCaller really saves the day.

Surviving the heat?

6.
Renato Carvalho says at

Nice Tips! I`ll pratice this.

Thanks!

7.
Nate Chatellier says at

Great post Arthur! Especially mentioning timeScale. Thanks!

8.
Gabriel Laet says at

- Yep, 40C right now - I'm preparing myself to the global heat, hehehe.

9.
Adam says at

I'm about to dive headfirst into Tweener so thanks for these tips! However, AS3 does have the setTimeout method in the flash.utils class:

import flash.utils.*;

setTimeout(myFunction, 300);

10.
Arthur Debert says at

:

The Timer class is preferred instead of the setTimeout, but it's still there in AS3: adobe's doc.

11.
devin M. arnold says at

Howdy Arthur,

I really love the fact that I can pause and resume tweens within the Tweener class...so when the time came to implement a pause and resume timer within a project I am developing I immediately thought of using Tweener...then I stumbled across your blog and realized it could actually be done.

Unfortunately when I tried your delayed function call or as you call it the "Poor Man's Timer"...I keep getting the following error:

ReferenceError: Error #1069: Property count not found on gameClasses.GameTimer and there is no default value.

I was under the impression that I didn't have to supply a property for the tweener in order to get the "Poor Man's Timer" to work.

Is there something I am missing? Do I need to supply at least one property for the tween to occur?

cheers,

devin M. arnold

12.
devin M. arnold says at

at caurina.transitions::Tweener$/caurina.transitions:Tweener::getPropertyValue()[D:SOFTWARE_DEVELOPMENTMGC_DEVELOPcaurinatransitionsTweener.as:881] at caurina.transitions::Tweener$/caurina.transitions:Tweener::updateTweenByIndex()[D:SOFTWARE_DEVELOPMENTMGC_DEVELOPcaurinatransitionsTweener.as:707] at caurina.transitions::Tweener$/caurina.transitions:Tweener::updateTweens()[D:SOFTWARE_DEVELOPMENTMGC_DEVELOPcaurinatransitionsTweener.as:572] at caurina.transitions::Tweener$/onEnterFrame()[D:SOFTWARE_DEVELOPMENTMGC_DEVELOPcaurinatransitionsTweener.as:921]

13.
devin M. arnold says at

Arthur,

Sorry i forgot to post the constructor of my class in the other message, here it is:

    public function GameTimer(delay:Number, repeatCount:Number)
    {
        Tweener.addTween(this, {time:delay, count:repeatCount, onComplete: onTimerComplete, onUpdate:onIntervalComplete});
    }

As you can see, I pass in a couple of values to pass into the addTween method of the tweener class. Could that be causing the problem with properties in the Tweener.as class?

Since this class does nothing on the timeline with sprites or movieclips is there a property i could use to satisfy this problem without causing any internal problems with Tweener.as?

thanks,

devin M. arnold

14.
josh says at

If one uses addCaller as an alternative to setInterval for repeating the call of a function iss there a way of pause or clear the repeating function. How would you even use removeTween in this situation?

Thanks. Josh

15.
josh says at

If one uses addCaller as an alternative to setInterval for repeating the call of a function iss there a way of pause or clear the repeating function. How would you even use removeTween in this situation?

Thanks. Josh

16.
josh says at

I appologize for double post .

17.
Chris says at

great post, thanks for these tips Arthur!

M. arnold: do a trace on "this" in your constructor function first and see if it has a value other than "undefined"! your class might not already be properly instantiated at this time!

like so: public function GameTimer(delay:Number, repeatCount:Number) { trace("GameTimer() :: this => " + this); Tweener.addTween(this, {time:delay, count:repeatCount, onComplete: onTimerComplete, onUpdate:onIntervalComplete}); }

18.
devin M. arnold says at

Thanks for the tip Chris!

Happy New Year

devin

19.
Arthur Debert says at

Hi Folks.

Apologies for not responding. I've just found out that the comments feed for the website had a bug in it, and I didn't get notified o new comments (damn writing you own blogging engine!)

As a general note, Tweener has a discussion list that's absolutely great for getting questions about tweener answered. I've never seen a question go unanswered over there.

: Thanks for helping out.

: Yup, pauseTweens on that object should work as well, see this thread .

cheers

20.
johnnyiller says at

Awesome post. I have been using tweener for over year and did not know about the nested templates that is incredibly useful... thanks

21.
Winstonian says at

Very Cool Stuff. However, I cannot get "Set Time Scale" script to work in my AS3 environment. Works great with AS2 but I've now converted everything to AS3. Am I missing something?....Regards. ww.

22.
sam says at

Hi im just starting with Tweener...the fast forward is great to have, but is it possible to set the tween to start immediately from any given moment within the tweens original lifecycle. For example start from 5 seconds into the tween...

23.
mr.hichem says at

check out those 2 new classes :

extendible Tweener class : http://halfrobot.wordpress.com/2008/04/15/basetweener-class/

ChainedTweener class : http://halfrobot.wordpress.com/2008/04/20/chainedtweener-class/

24.
Arthur Debert says at

:
The Tweener mailing list is the place to check out for this:

:
If I understand you correctly, no , not without some ugly hacking. The reason for this is that tweener is time based, and for that to work you'd have to alter the time started for that particular tween. Again the mailing list is the best place to ask around.

Hichem:
Thanks for the pointers.

25.
stardust says at

I've been trying this tip in AS3 but

var scaleFadeIn : Object = { alpha:0, time:1, transition: "linear" } Tweener.addTween(myMc, {base:scaleFadeIn});

the alpha is not working!

Can someone help me? I don't know why!

Thx stardust

26.
Arthur Debert says at

Hi There.

Tweener has a great mailing list which is a better place to get help than this blog entry.

Cheers Arthur

posted by
Arthur Debert

on

Tagged with:

Other months availabe in 2007

The complete blog archive

The latest entries

Subscribe to comments on this entry: rss feed

Cloud me:

Feeds: Entries rss feed Linksrss feed Worksrss feed

A Django joint. hosted on Slicehost