Scripted motion and inertia

Scripted motion and inertia                                                  

This is the fourth article in the series on creation movement in Flash with ActionScript. So far we’ve looked at moving an object in a straight line and in a circle using ActionScript. We’ve also looked at fading and rotating with code.

In this article, we’ll look at creating more realistic straight line motion. So far, the movement that we’ve created has used a constant speed. This is not very realistic as most of the motion in the real world consists of acceleration and deceleration. When an object moves, there is often inertia slowing it down. If you drove a car without your brakes on, inertia would decrease the speed of the car as you moved further away from your starting point. Similarly, when you drop an object from a height, gravity speeds it up as it gets closer to the ground.

By working through this tutorial, you’ll learn how to add inertia and gravity to your Flash objects. We’ll create functions that allow you to reuse your code throughout your Flash projects.

I've assumed that you are using Flash MX or Flash MX 2004 and that know how to add ActionScript to a movie. I've used ActionScript 1.0 for the examples.

You can download the source files for the tutorial from the blue Properties box that contains the article PDF. There's a heading titled Code Download and you can click the Details link next to it to get the zip file. The download includes the starter files you'll need as well as the completed files.

Note: If you have difficulties downloading the source files or PDF, you might have a problem with your cookies. Delete the cookies from your machine and try again. In Internet Explorer, you can do this by choosing Tools > Internet Options… and clicking the Delete Cookies… button on the General tab.

$2.89
- OR -

Overview

Inertia

Inertia is basically drag - the effect where the weight of an object slows down its speed. In Flash, we can mimic inertia by reducing the speed of an object as it gets closer to its target. The easiest way to do this is to figure out how much distance is between the start and finish points and divide the distance by an inertia value.

Open the file simpleinertia.fla. If you test the movie, you'll see that it moves to the right at a constant speed. Select the ball movie clip and open the Actions panel with the F9 shortcut key.

Sas Jacobs

Sas JacobsHello. I'm the Principal of Anything Is Possible, an Australian web development business specialising in web applications development and training. I'm interested in using Flash with dynamic content and I've presented at a number of International conferences on topics relating to applications development, XML and scripting components. I have recently released my second print book Beginning XML with DOM and Ajax to match the first one - Foundation XML for Flash. I have a business web site
and a personal web site.

See All Postings From Sas Jacobs >>

Reviews

Be the first to write a review

You must me logged in to write a review.