As a developer, I have run into situations where I wanted to make an object move, but its movement depended on other objects in the movie. A lot of times, this used to take forever; Building movie clip after movie clip with stop actions on certain frames. Well, by using onClipEvent scripts, this is much easier.
First, create a box just off the top of the stage. Press F8 to make it into a movieclip. Give it the instance name of my_box.
Now, create another, smaller box for a button. Press F8 to make it into a button.
Ok, Now, let’s make the first movieclip (my_box) ready to move. Click on the instance of my_box on the stage. Open the actions panel and enter the following code:
onClipEvent(load){
// Set the variables up to be equal to where the clip is now.
xDest = this._x;
yDest = this._y
aDest = this._alpha;
}
onClipEvent(enterframe){
// See if the xDest has been changed
if (this._x != xDest) {
this._x += (xDest-this._x)*.2;
}
// See if the yDest has been changed
if (this._y != yDest) {
this._y += (yDest-this._y)*.2;
}
// See if the aDest has been changed
if (this._alpha != aDest) {
this._alpha += (aDest-this._alpha)*.2;
}
}
Ok, our box is ready to be moved. Now, let’s make the button do the moving. Click on the button you created and open the actions panel. In the panel, enter the following code:
on(release){
_root.my_box.yDest = 100;
}
Ok, now save your file and test your movie. You will see that when you click the button, the box slides. You can also change the xDest to change the x coordinate or you can change the aDest which changes the alpha.
You can add other “Dest” variables to make the clip do more things like scale or rotate.
Also, notice the “*.2” part in the onClipEvent(enterframe) block. You can change this number to make the easing on the clip faster or slower. Play with it to make the movement the way you want it to look.
Also, duplicate the button on the stage, and change the code on it to be:
on(release){
_root.my_box.yDest = -200;
}
Now, you can click one button to make the box slide down and another to make it slide up.
To download the flash movie for this tutorial, click here.