Now lets make the clip scroll
to different positions. Remove all the
actions on frame 1 and add these-
ball._x=50;
ball.onEnterFrame=function(){
//this variable stores the current x position of the clip
cX=this._x;
//this variable stores the distance between the clips current x position
//and where we want it to go
//I am setting the targX variable in the root so that it is easy to target
with our button
difX=cX-_root.targX;
//this moves the clip 1/5 of the distance every frame.
//Because the difference between the clips current location and its destination
//will get smaller each frame, so will the amount the clip travels each
frame.
//This is what makes the clip appear as though it "eases into" it's
final destination
setProperty(this, _x, cX-(difX/5));
//check the flash manual to learn more about the setProperty action
}
button1.onRelease=function(){
//this sets the targX variable to a random number between 0 and 500
_root.targX=random(500);
}
Test your movie and click the button
a few times. You can use this formula
on the movieclip to get some great effects.
Try adapting it to effect the clips x
and y scale. You should end up with something
like this:
_root.targXscale=50;
_root.targYscale=200;
ball.onEnterFrame=function(){
cXscale=this._xscale;
cYscale=this._yscale;
difXscale=cXscale-_root.targXscale;
difYscale=cYscale-_root.targYscale;
setProperty(this, _xscale, cXscale-(difXscale/5));
setProperty(this, _yscale, cYscale-(difYscale/5));
}
button1.onRelease=function(){
_root.targXscale=random(500);
_root.targYscale=random(500);
}
Test your movie. You will notice that
even if you click the button quickly
the scaling of the clip never jumps -
imagine trying to do that with tweened
animation!
This code works fine and is easy to
read but if you want to clean it up change
it to the code below, which does the
same thing:
//set the starting
value for 2 variables
targXscale = 50;
targYscale = 200;
ball.onEnterFrame = function() {
//smooth scaling code
this._xscale-=(this._xscale-targXscale)/5;
this._yscale-=(this._yscale-targYscale)/5;
};
button1.onRelease = function() {
//set the 2 vars to random values
_root.targXscale = Math.random()*500;
_root.targYscale = Math.random()*500;
};
That's it. Have fun experimenting with
this code.
|