Realistic gravity effect in flash

Date: 10.8.2009, 13:6    Total views: 55822

Read this thoroughly explained, detailed flash lesson and see how to create realistic gravity effect using the Action Script code and arrows key. Using this lesson, you will also learn how to convert any object into a movie clip symbol, how to apply action script code on it... Let's start!


Step 1

Create a new flash document. Press Ctrl+J key on the keyboard (Document Properties) and set the dimensions of your document as whatever you like. Select any color as background color. Set your Flash movie's frame rate to 33 and click ok.

Step 2

Draw any object which you like to use for this lesson.

Step 3

While the object is still selected, hit F8 key (Convert to Symbol) to convert it into a Movie Clip Symbol.

Step 4

Select the Selection Tool (V) and click once on the object to select it. After that, go to the action script panel (F9) and enter this code inside the actions panel:

onClipEvent (load) {
 acceleration = 0.4;
 yspeed = 0;
 xspeed = 0;
 friction = 0.95;
 gravity = 0.8

onClipEvent (enterFrame) {
 if (Key.isDown(Key.LEFT)) {
  xspeed -= acceleration;
 if (Key.isDown(Key.RIGHT)) {
  xspeed += acceleration;
 if (Key.isDown(Key.UP)) {
  yspeed -= acceleration;
 if (Key.isDown(Key.DOWN)) {
  yspeed += acceleration;
 xspeed *= friction;
 yspeed += gravity;
 _y += yspeed;
 _x += xspeed;

We're done now!

Test your movie and enjoy!

Download source file (.fla)

  Digg it! Add this tutorial to! Furl it! Add this tutorial to reddit! Spurl it! Add this tutorial to technorati!