PDA

View Full Version : Make movieclip width slowly expand?



sciguy77
September 10th, 2009, 10:44 PM
I have a movieclip, powerMeter, that I need to slowly expand until it's width is 80. I using
powerMeter.width -= 80; to make it shrink, but I need a way to slowly make it scale back up.

Any ideas?

Thanks.

IQAndreas
September 11th, 2009, 03:41 AM
You could have it keep expanding by a little bit each frame until it reaches 80 pixels.

But it would be even faster, smoother, and nicer if you "tweened" the action. A tween engine asks you to pass in an object, how long time you want it to run, and finally, which properties to tween and what you want the final value to be.

One tween engine I really like using is "TweenLite". The library is free and can be found and downloaded here:
http://blog.greensock.com/tweenliteas3/ (http://blog.greensock.com/tweenliteas3/)
(I should get some sortof comission for how many beginners I have sent towards TweenLite. ;)

Then it's as simple as doing like this, which Tweens powerMeter for one second until it is 80 pixels wide.

//Don't forget to import the class. :)
import gs.TweenLite;

TweenLite.to(powerMeter, 1, {width:80});

sciguy77
September 11th, 2009, 03:57 PM
Is there a way to do this without tweening?

IQAndreas
September 11th, 2009, 05:57 PM
powerMeter.addEventListener(Event.ENTER_FRAME, reduceWidth);
function reduceWidth(ev:Event)
{
powerMeter.width += 5;

if (powerMeter.width >= 80)
{
powerMeter.width = 80;
powerMeter.removeEventListener(Event.ENTER_FRAME, reduceWidth);
}
}

Personally, I prefer one line of code to 6.