Latest tutorial: Making a Movieclip face another Movieclip or point on the stage | Ask Tutorial5!
 

Moving an object using the arrow key in Adobe Flash 8

(28 votes)
Written by AnaS   

1. Create a new Flash8 document with 400x300 dimensions. Save it as flash8keys.fla.
2. Insert a new layer. Rename layers actions and object like in the picture below:
flashkey1.gif
3. In the first frame of the object layer go to File>>Import>>Import to Stage (Ctrl+R) and insert the smiley picture below:
flashkey2.gif

4. Select the smiley picture and choose Modify>>Convert to Symbol (F8). In the Convert to Symbol window choose MovieClip as Type, asuure that registration point is center and give it smileyMC name. Now go in the Properties panel and give it an instance name of smiley_mc.

5. In the first frame of the actions layer choose Window>>Actions (F9) and insert the ActionScript code below:

var KeyListener:Object = new Object();
KeyListener.onKeyDown = function():Void {
if(Key.isDown(Key.LEFT)){
if (smiley_mc._x>50){
smiley_mc._x-=5;
}
}
if(Key.isDown(Key.RIGHT)){
if (smiley_mc._x<350){
smiley_mc._x+=5;
}
}
if(Key.isDown(Key.UP)){
if (smiley_mc._y>50){
smiley_mc._y-=5;
}
}
if(Key.isDown(Key.DOWN)){
if (smiley_mc._y<250){
smiley_mc._y+=5;
}
}
};
Key.addListener(KeyListener);

6. Now let's see what the previous ActionScript code do. First we create a Flash listener object to listen the onKeyDown event. onKeyDown() event handler is invoked when a keyboard key is pressed.

If a key is pressed we analize if one of the LEFT, RIGHT, UP or DOWN key is pressed.
Let's take the case f the UP key (all the other cases are similar).

if(Key.isDown(Key.UP)){
if (smiley_mc._y>50){
smiley_mc._y-=5;
}
}
The smiley_mc._y is verified so that the movieclip not to get out of the stage. Then if the condition is realized we substract 5 from _y of smiley_mc.

7. Test the movie (Ctrl+Enter) now. Hope this flash tutorial helps you!

You need to Register in order to download the FLA file.



Subscribe now via RSS feed and get all the new tutorials

written by bruce montford , September 19, 2007

ikeep getting this error message when i test my movie

1046: Type was not found or was not a compile-time constant: Void.
written by AnaSt , September 19, 2007

In this tutorial it is used Flash 8 and ActionScript 2. What do you use?
written by coz , November 20, 2007

this really helped me - thanks very much
written by lil lee , December 11, 2007

it dont work
written by Ken , December 13, 2007

It really helps thanks
written by Flashy Games , December 24, 2007

It dosn't work Y
written by Flashy Games , January 13, 2008

Now it works thanks.
written by Toon , June 14, 2008

it won't work
Scene=Scene 1, Layer=actions, Frame=1: Line 2: '{' expected
KeyListener.onKeyDown = function():Void {
written by Venkadesan Tharshan , October 09, 2008

Thanks
written by DavidMex , May 20, 2010

thank you so much!!!!!
written by dude1 , July 12, 2010

can u pls help me with this? am trying to create a project that revolves around menu bars.. i want that when i press my left arrow key lets just say i have menus like HOME, PICTURES, HELP, EXPLORE i want that if i press left arrow key a highlight will move to pictures vice versa.. pls help thanks alot!!
written by ghd , July 26, 2010


this really helped me - thanks very much

Do you need more help? Ask now!
 

busy
Last Updated ( Friday, 28 March 2008 )