Moving objects with ActionScript part 1

You're probably familiar with motion tweens and using them to create movement in a Flash movie. Motion tweens move objects around the Stage, rotate them and fade them in and out. What you might not know is that you can achieve the same effect using ActionScript. This approach gives you much more flexibility and you can write functions that you can reuse in your Flash projects.

This is the first in a series of articles looking at scripted motion in ActionScript. In this article, we'll start by creating simple motion in a straight line. We'll look at how you can bounce an object off the walls of a movie clip.

The other articles will be on:

  • Circular motion
  • Fades and rotations
  • Inertia and gravity effects
  • Creating springs
  • Tweening and Transition classes

By the end of each article, you'll be able to create a function that you can reuse throughout your movies. I

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 my 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.79
- OR -

Overview

Movement concepts

Moving an object in ActionScript consists of a series of small movements to the x and y co-ordinates of an object. There is normally a calculation to find the new position and this is repeated to give the effect of moving the object.

We can use an enterFrame event to repeat the movement calculation. Remember that movie clips are constantly playing at the frame rate of the movie. If the frame rate is 12fps, the enterFrame event will run 12 times each second. Anything inside an enterFrame function will also run 12 times per second. By adding code to the onClipEvent(enterFrame) event handle, we can create movement.

 

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.