Tweener Tips

A few lesser known tips for Tweener.

I've been using Tweener 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

@og2t: 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.

@Gabriel 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

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

10.
Arthur Debert says at

@Adam:

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

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.

@Chris: Thanks for helping out.

@josh: 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

24.
Arthur Debert says at

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

@sam:
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.

@Mr Hichem:
Thanks for the pointers.

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:
  • Rafael Cordoba in The Web does not need flash. on

    I think Flash has great potential when used the right way. A Flash application/website is supposed to be an experience for the user, the focus is not pure content.

    ...

  • ash in The Web does not need flash. on

    I've been a Flash/Flex developer for several years. I love the development environment and the pay cheque. However, every Flash/Flex developer I talk to agrees that Flash's days are numbered.

    ...

  • Gabriel Laet in The Web does not need flash. on

    @Carlos I agree with you about 2D games. But the thing is that Flash is not the only tool for that, even Unity3D is a great tool for 2D games. ...

  • Carlos Nazareno in The Web does not need flash. on

    @Gabriel, Unity3D is fantastic, but only for 3D. You need to re-think your views about games. 3D is not the be-all and end-all of gaming. Far from it, as the ...

  • Ricardo in The Web does not need flash. on

    I fully disagree. Flash is years ahead HTML 5 and all it´s infinite coming bugs across the different browsers. Flash continues to evolve features and performance as well. Besides of ...

Feeds: Entries rss feed Linksrss feed Worksrss feed

A Django joint. hosted on Slicehost