First time here? First time here? Newsletter Signup Newsletter
Search tutorials
Author: Luke Knepper | Website: http://zazeran.com

How to move a sprite with the keyboard.

Hi. When I was starting off in Flash 8 I couldn't find any good games tutorials that worked, so I decided to start writing some once I figured out the basics of Flash 8. This is a technique that was especially hard for me to find out how to do without buying an expensive book or taking a course. It will show you how to make a sprite and animate it on the screen and move it using the arrow keys.

To Start you will need: -Flash 8 -a new Flash document

first we are going to make a sprite. So make a new symbol by going Insert>New Symbol. Then choose movie clip and name the symbol whatever you want. A symbol is like an object that you can apply actions, code, and animation to in Flash. In the area that opens up you can draw your sprite. It can be as simple as a ball or as complex as a real game sprite, but for learning purposes just make it a circle or a square. If you wanted to animate it you would just insert keyframes in the timeline at the appropriate spots by right clicking (command clicking on Macs) in the appropriate frames, and if you wanted to fill in motion you would apply a motion tween by right clicking inbetween the two keyframes and choosing insert motion tween. The motion tween will fill in the frames with the appropriate changes in position or size in a single symbol/shape.

Now that you have your sprite we will need to apply code to it in the main timeline.

Go to the main timeline by clicking scene 1 above the timeline. Then, drag the symbol onto the white spot of the stage. This inserts the symbol into your main movie/game. Click on the sprite to make sure you have it selected and press F9 or Windows>Actions. This brings up the actions panel. Now you should type in the following code:

onClipEvent(load) { if(Key.isDown(Key.UP)) this._y-=5 if(Key.isDown(Key.DOWN)) this._y+=5 if(Key.isDown(Key.LEFT)) this._x-=5 if(Key.isDown(Key.RIGHT)) this._x+=5 }

Here is what the code does: First it checks to see if the movie clip is loaded (this or something like it is required to add code to a movie clip in Flash 8). Next it checks first if the up key is down, then the down key, followed by the left and then right keys. If any of them are down it changes the sprite's position accordingly. Now you will see that the sprite moves around the screen when you press the arrow keys. I hope that you have found this tutorial useful. Please email me at luke@zazeran.com if you find any errors or have any comments or suggestions on/in this tutorial. Thanks!
» Level Basic

Added: 2006-07-16
Rating: 3.74 Votes: 27
» Author
Luke Knepper has been using Flash 8 for almost a month at the time that this tutorial was written and had trouble finding good flash tutorials. He also maintains his website, creates websites for others, and programs in Java as tech hobbies.
» Download
Download the files used in this tutorial.
» Forums
More help? Search our boards for quick answers!
Featured Flash FLA
» Author: Inocreato
» Title: RaiseTheBlocks
» Description: Raise all the blocks to win the game
Featured Sound Loops
Image for equalize

Audio Player

» Author: TomCat Carty
» Title: The Wood
» Description: Just a little game ending or it can maybe be looped. Recorders with music box and percussion to give the feel of well, I don't know, the woods? Free to use, just credit me. thank you
Latest Font
» Author: Fábio FAFERS
» Description: I created this font for free use. Everyone can apply it in personal or business texts. Its free, but I want to be communicated in case of business use. Donations are accepted to keep the project of free fonts alive! Thank you all
Featured Sound Fx
Image for equalize

Audio Player

» Author: Davisigner
» Description: Hmm... what to say about this one? It's reminiscent of the closing notes of the opening music from the Three Stooges done in a church organ style with a closing cymbal crash. I'll give this one away gratis, but feel free to check out my free loops and potential upcoming license-mandated ones over in the respective part of Flashkit.