In this tutorial you will learn how to create basic 8 directional movement using the arrow keys.
First, open a new flash file. (If you are using CS3, make sure it is an AS2 file!)
On the stage, create a shape like this:

Select this shape and press f8. You should get a box which looks like this, it is the “Convert to symbol” box:

Name it the name of object1_ Mc and set the Registration to center. Press Ok.
Next, click on the object again and then in the top menu go to windows>properties or press Control + f3. You will get a menu similar to this, dependant what Flash version you are using:

Where it says “<Instance Name>”, type object_MC. This is the name of the object when its referred to in Action Script.
Now click on the object again and hit f9 to bring up the Actions screen. Once in the Actions screen write the following code:

I have put this code here as a screenshot so you cannot cut and paste, this way it will stick more firmly in your mind!
Once completed, press ctrl+enter.Now when you use the arrow keys, you can move the object you created in 8 directions!
I will now explain the code to show you how it works:
On line 1, onClipEvent (enterFrame) { means that this frame of code will be triggered every time this frame is entered, in my case its 12 frames per second.
Lines 2-4 are in a if statement. They check that when the up key is pressed down, the object will move 10 pixels up the y axis. This code is broken down as follows:
_y is the axis
-= means it will subtract the number on the right.
10 is the number of pixels to subtract
{ is the start of the executed code
} is the end of the executed code
lines 5-7, 8-10 and 11-14 are all the same but some of them use the _x axis and the += operator.
Change the code around play with it a bit see what you can do!
Happy game making!
Tutorial by Josh Beynon
Did you find this tutorial useful? Do you have any questions about the tutorial or a request for another? Please sign up now and post your comments below!
Did this post help you? If so help us continue to grow and Digg this page!
If you haven't already, sign up to our RSS feed or register to recieve updates direct to your email!








Great working example! Although I suggest putting all AS code on frame 1 of the main timeline, or better yet, in separate .as files.
Sticking code to separate objects on the stage can make it hard to find specific code and then edit it, especially when your game grows really large.
The script of the above example can easily be put on the main timeline by using an instance name for the moving object:
myInstance.onEnterFrame = function(){
// key press code here
}