This tutorial is about how to move an object using the LEFT, RIGHT, UP and DOWN heys in Adobe Flash 8. 1. Create a new Flash8 document with 400×300 dimensions. Save it as flash8keys.fla.
2. Insert a new layer. Rename layers actions and object like in the picture below:
![]()
3. In the first frame of the object layer go to File>>Import>>Import to Stage (Ctrl+R) and insert the smiley picture below:

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!
RSS feed for comments on this post · TrackBack URI
Leave a reply