Subscribe to this Blog

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:

movieClip

Then I created a MovingObject2.as and inserted the following code in it:

  1. package {
  2.     import flash.display.*;
  3.     import flash.events.*;
  4.     public class MovingObject2 extends MovieClip {
  5.         var hero1:hero = new hero();
  6.         var speed:int=5;
  7.         var ldown:Boolean=false;
  8.         var rdown:Boolean=false;
  9.         var udown:Boolean=false;
  10.         var ddown:Boolean=false;
  11.         public function MovingObject2() {
  12.             hero1.x=275;
  13.             hero1.y=200;
  14.             addChild(hero1);
  15.             stage.addEventListener(KeyboardEvent.KEY_DOWN, keydown);
  16.             stage.addEventListener(KeyboardEvent.KEY_UP, keyup);
  17.             addEventListener(Event.ENTER_FRAME, moveHero);
  18.         }
  19.         function keydown(event:KeyboardEvent) {
  20.             if (event.keyCode==37) {
  21.                 ldown=true;
  22.             } else if (event.keyCode == 39) {
  23.                 rdown=true;
  24.             } else if (event.keyCode == 38) {
  25.                 udown=true;
  26.             } else if (event.keyCode == 40) {
  27.                 ddown=true;
  28.             }
  29.         }
  30.         function keyup(event:KeyboardEvent) {
  31.             if (event.keyCode==37) {
  32.                 ldown=false;
  33.             } else if (event.keyCode == 39) {
  34.                 rdown=false;
  35.             } else if (event.keyCode == 38) {
  36.                 udown=false;
  37.             } else if (event.keyCode == 40) {
  38.                 ddown=false;
  39.             }
  40.         }
  41.         function moveHero(event:Event) {
  42.             if (ldown) {
  43.                 hero1.x-=speed;
  44.             }
  45.             if (rdown) {
  46.                 hero1.x+=speed;
  47.             }
  48.             if (udown) {
  49.                 hero1.y-=speed;
  50.             }
  51.             if (ddown) {
  52.                 hero1.y+=speed;
  53.             }
  54.         }
  55.     }
  56. }

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.

If you liked this post, Buy me some beer (or coffee)

Tagged as: , , , , , , , , ,

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

Leave a Reply