1. Code
  2. Games

Quick Tip: Easy AS3 Character Movement With

Read Time:3 minsLanguages:

In this tutorial I will introduce a class by that allows easy movement of game characters with minimal code.

Final Result Preview

In the SWF you'll see a spaceship; use your Left, Right, Up, and Down arrow keys to move it.

Step 1: Explanation of

When ActionScript 3.0 came out we lost the functionality of AS2's Key.isDown() method. Senocular has coded a great little class that will let us emulate this functionality within actionscript 3 and that is what we will look at in the tutorial.

Step 2: Setting Up the Project

Go to File > New and create a new Actionscript 3.0 document, with the following properties:

  • Size: 550 * 400
  • Background Color: White
  • FPS: 24

Save this file as "KeyObject.fla"

Step 3: Downloading

Before we can code our application we need to get the "" file, so head over to Under the Flash Menu, click on Actionscript. Once there you will want to drill down to "" and download it. Get there by going to Actionscript 3.0 > com > senocular > utils.

KeyObject Flash tutorial for AS3

You can right-click on the download link and save it as "".

Once you have done this you need to remove com.senocular.utils right after the package declaration in the file, since we are not using the com.senocular class path.

Change this:

To this:

Step 4: Importing the Player Graphic

In the download files there is a spaceship image called player.png. In Flash, import this to the stage, by going to File > Import > Import To Stage. Right-click on it and choose "Convert To Symbol", give it the symbol name "player", and make sure the registration point is set to the top left. Now give it the instance name of "player" as well.

KeyObject Flash tutorial for AS3KeyObject Flash tutorial for AS3KeyObject Flash tutorial for AS3
KeyObject Flash tutorial for AS3

Step 5: Setting Up the Main Class

Go to File > New and choose ActionScript File.

KeyObject Flash tutorial for AS3

Save this as and set it as your Document Class within "KeyObject.fla".

Next add the following code to "":

Here we set up our package and import the classes we will be using. Next we set up the key variable as type KeyObject, and within our Main constructor we add an ADDED_TO_STAGE Event Listener. This gets called when the movie is fully loaded and the stage is ready.

Inside the setupKeyObject function, we set the key variable to be a new instance of the KeyObject class and add an ENTER_FRAME Event Listener to the stage.

Within the movePlayer function we check which key is being pressed by using key.isDown() and move our player accordingly.

Finally, we check to see whether the object has moved outside the bounds of the stage, and if it has we put it back just inside the stage.


Using Senocular's KeyObject class makes it dead simple to move your game characters! I hope this tutorial has helped; thanks for reading.

Looking for something to help kick start your next project?
Envato Market has a range of items for sale to help get you started.