Advertisment

Scripting Flash Animations

author-image
PCQ Bureau
New Update

There are a lot of instances in Flash where just tweening does not help. The developer has to use scripting in order to animate objects. 

Advertisment

One of the examples of the same instance is when you may want to change the speed of the moving object, dynamically in a movie. 

Don’t be disheartened, because actionscript in Flash is robust enough to handle simple tasks like these. 

Lets plunge a bit deeper and find out how we can accomplish this with Flash. We will be using Macromedia Flash MX for the tutorial.

Advertisment

Though it may not be necessary for you to know actionscript, but a little exposure to some programming language is desirable.

Any kind of animation can either be time based or frame based. As the name suggests, time based animation drops frames on slower machines in order to finish the animation in the specified time. 

On the other hand, frame based animation completes animating all the frames even though it may take some more time on slow machines. In this tutorial we will be learning how to script a frame-based animation. We would be simply moving a ball from the bottom left corner of the screen to the top-right corner.

Advertisment

    STEP

ONE 
  
 



Make any object on the screen that you would like to animate. For example you can use a simple ball or a circle. 

Change it into a movie clip, and name it ball-mc in the dialog that pops up. In the properties panel, name the instance of the ball to ball.

    STEP

TWO 
  
 



Firstly we will need to initialize the x and y position of the ball. It can be done using the following script.

Advertisment

onClipEvent (load) {



xpos = 0;


ypos = 400;


_x = xpos;


_y = ypos;


}



Here, xpos and ypos are the variables used for calculating the position of the ball after every frame.

    STEP

THREE 
  
 



Now we would change the position of the ball with every frame. This will be done using the OnClipEvent

(enterFrame) event.

Advertisment

onClipEvent (enterFrame) {



xPos += 55;


yPos -= 40;


_x = xPos;


_y = yPos;


}



    STEP

FOUR 
  
 



Using the earlier script we were able to animate the ball using some absolute values. Lets take this a step further and change the speed of the ball as it moves along. For doing

this we would use a couple of variables for speed. We would also 



need another variable that will change the speed. The three new variables will be speed, xSpeed and ySpeed. The scripts written earlier will be changed to the following script.

onClipEvent (load) {



xSpeed = 11;


ySpeed = 8;


Speed = 2;


xPos = 0;


yPos = 400;


_x = xPos;


_y = yPos;


}


onClipEvent (enterFrame) {


xSpeed += Speed;


ySpeed += Speed;


xPos += xSpeed;


yPos -= ySpeed;


_x = xPos;


_y = yPos;


}














Advertisment

Press Ctrl+Enter to check the movie. 

The sample file for this tutorial is there in this month’s PCQEssential CD in the folder system\cdrom\tutorials\multimedia. The file is called speed up.fla. Happy scripting!

Geetaj Channana

Advertisment