Flash Keyboard Input

This week in my graduate Interactive production class, we learned how to use input of the keyboard up, down, left, and right arrow keys. We also learned how to use dynamic text to create timers and score boxes.

So, I decided to recreate a simple space game in which a spaceship has to make it to the safety point (located on the other side of the stage), while avoiding asteroids. The spaceship can shoot the asteroid thereby making the task easier. If an asteroid hits the spaceship, points are deducted from its score; if the score falls below 0, the player loses. Sounds simple right? But creating it in Flash was not as simple as paying the game

First, I used keyboard input ActionScript code that allows the user to use the arrow keys on the keyboard to move the spaceship around. I had to figure out how to get the spaceship to shoot a bullet that could hit the asteroid. I created a red dot that hid behind the ship. When activated (pressing the space bar) the red dot (“bullet”) traveled a certain distance in a certain amount of frames. I created the effect by making the red dot a movie clip called bullet. I used the same ActionScript that I used to make the ship move up and down with the arrow keys; however this time the key code was changed to indicate the pressing of the space bar. The ActionScript specifies when the space bar is pressed, go to and play this red dot movie clip. The ActionScript looked like this.

stage.addEventListener(KeyboardEvent.KEY_DOWN, move_object2);
function move_object2 (e:KeyboardEvent):void{

if (e.keyCode == Keyboard.LEFT){
test1.rotation = -180;
test1.x -=25;
}

if (e.keyCode == Keyboard.RIGHT){
test1.rotation = 0;
test1.x +=25;
}

if (e.keyCode == Keyboard.UP){
test1.rotation = 0;
test1.y -=25;
}

if (e.keyCode == Keyboard.DOWN){
test1.rotation = 0;
test1.y +=25;
}

if (e.keyCode == Keyboard.SPACE){

test1.play();
}
}

The red dot bullet clip hides behind the space ship, and when users use the arrow keys to move the spaceship, the red dot movie clip moves as well. Then when the space bar is pressed, the “bullet” fires and destroys the asteroid.

This little project was a lot of work because I had to learn how to use new codes of ActionScript I had never used before. All though very simple, I enjoyed this project and I look forward to using some of the techniques I learned to create more advanced Flash games!

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s