Monthly Archive for October, 2007

You and a Tween should get a room!

If you're not using the Tween Class that flash provides, today is a great day to start. ESPECIALLY if your code looks something like this:

Actionscript:
  1. oldBustedTween(350);
  2.  
  3. function oldBustedTween(val:Number)
  4. {
  5. this.onEnterFrame = function()
  6. {
  7. if(Math.abs(myShape._x - val)<1)
  8. {
  9. myShape._x = val;
  10. this.onEnterFrame = null;
  11. }
  12. else
  13. {
  14. myShape._x += (val - myShape._x)*.2;
  15. }
  16. }
  17. }

At least this is how I used to move objects around. Today, and for the past several years you have had the Flash Tween Class available to you. It does all the work as you see above only it is much simpler to read.

After creating a new flash document follow these steps:

1) Create a shape and make it into a movieclip

2) Give your movieClip an instance name of myShape

3) Add this actionscript to frame 1

Actionscript:
  1. import mx.transitions.easing.*;
  2. import mx.transitions.Tween;
  3.  
  4. myAwesomeTween(350)
  5.  
  6. function myAwesomeTween(val:Number)
  7. {
  8. var myTween = new Tween(myShape, "_x", Strong.easeOut, myShape._y, val, .9, true);
  9. }

4) Save and test your movie.

Not too shabby right!

Basically the tween class is taking in all the information you give it and running all the calculations for you. Here is a closer look at each part.

From the help docs it looks like this :

Tween( obj:Object, prop:String, func:Function, begin:Number, finish:Number, duration:Number, useSeconds:Boolean )

My longer version:

1) Name of movieClip instance
Such as myShape

2) What do you want me to do
_x - _y - _alpha

3) When I do it what kind of effect
Use - Strong.easeOut / Regular.easeOut / Strong.easeIn etc.

4) Where do you want me to start
Use a number

5) Where do you want me to end up
Use a number

6) How fast in seconds or frames (see 7)
Use a number such as .5 or 10

7) Use seconds (if you put false it will assume you are counting in frames instead of seconds)
true or false

var myTween = new Tween(1, 2, 3, 4, 5, 6, 7)

Actionscript:
  1. var myTween = new Tween(myShape, "_x", Strong.easeOut, myShape._y, val, .9, true);

Once you start using the Tween class you will never go back. It is one of the easiest ways to get things to move. It also has ton's of other cool features to get the effects you want. Test out out.

Download the fla

Check out this site for a more in depth discussion of the Tween Class.

http://www.kirupa.com/developer/actionscript/tween.htm

What happened to the good-ole message board

I remember a time when you could go onto a message board, post a message and actually get an answer. Recently (within the last 2 years) whenever I go to places such as flashkit.com or other flash forums with a question I get one of three responses:

1) You're an idiot (or maybe in not so many words usually followed up by #2)

2) This has been answered a billion times, go search for it

3) no response at all

None of which were the answer to my question. I see this more and more. Is it that there are so many amazing flash people out there that all questions are beyond them. I doubt it is due to their lack of time because the next message down is thesis written about how much they hate the guy that asked a stupid question or about how great they look while coding flash.

Anyone else have this response on message boards?

I'd love to bring the days back when forum "gods" weren't jack@$$es. :)


Follow papervision2 on Twitter

RSS Feed