Actionscript 3.0 Basics: Handling Keyboard Events
By admin • Aug 12th, 2009 • Category: Tutorials
Now that you can create a class(shown here) and handle events(as shown here). It’s time for some user interaction. Now in this tutorial I’ll be showing how to make a simple circular movieclip move around using the arrow keys.
First I created a MovingObject2.fla with the following movieclip:

Then I created a MovingObject2.as and inserted the following code in it:
-
package {
-
import flash.display.*;
-
import flash.events.*;
-
public class MovingObject2 extends MovieClip {
-
var hero1:hero = new hero();
-
var speed:int=5;
-
var ldown:Boolean=false;
-
var rdown:Boolean=false;
-
var udown:Boolean=false;
-
var ddown:Boolean=false;
-
public function MovingObject2() {
-
hero1.x=275;
-
hero1.y=200;
-
addChild(hero1);
-
stage.addEventListener(KeyboardEvent.KEY_DOWN, keydown);
-
stage.addEventListener(KeyboardEvent.KEY_UP, keyup);
-
addEventListener(Event.ENTER_FRAME, moveHero);
-
}
-
function keydown(event:KeyboardEvent) {
-
if (event.keyCode==37) {
-
ldown=true;
-
} else if (event.keyCode == 39) {
-
rdown=true;
-
} else if (event.keyCode == 38) {
-
udown=true;
-
} else if (event.keyCode == 40) {
-
ddown=true;
-
}
-
}
-
function keyup(event:KeyboardEvent) {
-
if (event.keyCode==37) {
-
ldown=false;
-
} else if (event.keyCode == 39) {
-
rdown=false;
-
} else if (event.keyCode == 38) {
-
udown=false;
-
} else if (event.keyCode == 40) {
-
ddown=false;
-
}
-
}
-
function moveHero(event:Event) {
-
if (ldown) {
-
hero1.x-=speed;
-
}
-
if (rdown) {
-
hero1.x+=speed;
-
}
-
if (udown) {
-
hero1.y-=speed;
-
}
-
if (ddown) {
-
hero1.y+=speed;
-
}
-
}
-
}
-
}
Now let me explain the code:
By now you are familiar with the import statements so I’ll directly jump into the class MovingObject2:
LINE5: Creates a new movieclip hero1 of type ‘hero’.
LINE6: Defines the speed of movement used throughout this tutorial.
LINE7-10: Define Boolean variables that hold the state of the arrow keys (True if pressed and false if not pressed)
LINE11: The Constructor function for MovingObject2 class.
LINE12-14: Adding the movieclip hero1 to the stage at defined x and y co-ordinates.
LINE15-17: Adding three event listeners, one for the event of a key press, another for an event of key release and the third that triggers everytime an enter-frame occurs.
LINE19: Defines the function to be executed when a key press event is detected.
LINE20-27: Inserts a true value to the Boolean variables according to which keys are pressed.
LINE30: Defines the function to be executed when a key release event is detected.
LINE31-38: Inserts a true value to the Boolean variables according to which keys are NOT pressed.
LINE41: Defines the function that executes on every enter-frame.
LINE42-52: Adjust the co-ordinates of hero1 according to what keys are pressed.
Here is what the result looks like:
I guess the most important parts of this tutorial are knowing the keywords like “event.keyCode”, “KeyboardEvent.KEY_DOWN” and “KeyboardEvent.KEY_UP”
I hope this helped. In the next tutorial I’ll be taking up a little more advanced topic. You may download the source files for this tutorial here.
admin is An engineering student from India. Enjoying his life both in the real world and in cyberspace
Email this author | All posts by admin
