In this tutorial I will introduce a class by Senocular.com 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 KeyObject.as
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 KeyObject.as
Before we can code our application we need to get the "KeyObject.as" file, so head over to Senocular.com. Under the Flash Menu, click on Actionscript. Once there you will want to drill down to "KeyObject.as" and download it. Get there by going to Actionscript 3.0 > com > senocular > utils.
You can right-click on the download link and save it as "KeyObject.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:
package com.senocular.utils { import flash.display.Stage; import flash.events.KeyboardEvent; //Rest of Class
To this:
package { import flash.display.Stage; import flash.events.KeyboardEvent; //Rest of Class
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.
Step 5: Setting Up the Main Class
Go to File > New and choose ActionScript File.
Save this as Main.as and set it as your Document Class within "KeyObject.fla".
Next add the following code to "Main.as":
package { import flash.display.Sprite import flash.events.Event; import KeyObject; public class Main extends Sprite{ private var key:KeyObject; public function Main() { addEventListener(Event.ADDED_TO_STAGE,setupKeyObject); } function setupKeyObject(e:Event){ key = new KeyObject(stage); stage.addEventListener(Event.ENTER_FRAME,movePlayer); } function movePlayer(e:Event){ if(key.isDown(key.LEFT)){ player.x -= 5; } if(key.isDown(key.RIGHT)){ player.x +=5; } if(key.isDown(key.DOWN)){ player.y +=5; } if(key.isDown(key.UP)){ player.y -=5; } if(player.y<0){ player.y =0; } if(player .y > (stage.stageHeight - player.height)){ player.y = stage.stageHeight - player.height; } if(player.x<0){ player.x = 0; } if(player.x > (stage.stageWidth - player.width)){ player.x = stage.stageWidth - player.width; } } } }
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.
Conclusion
Using Senocular's KeyObject class makes it dead simple to move your game characters! I hope this tutorial has helped; thanks for reading.
Comments