First time here? First time here? Newsletter Signup Newsletter
Login | Register | 651326 Members and growing!
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.28 Votes: 25
(10 being the highest)
» 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!

Comments

  • There are no comments yet. Be the first to comment!

  • You must have javascript enabled in order to post comments.

Leave a Comment
  • Your email address will not be published. All fields are required.
Featured Flash FLA
» Author: work2survive
» Title: Girl Walking 1kb
» Description: Walking girl, animation just takes 1kb, using high heels shoes, and fashion model attitude.
Featured Sound Loops
Image for equalize

Audio Player

» Author: BoxCat
» Title: CPU Talk
» Description: From the cyberpunk RPG, "Nameless: the Hackers" Completely Free to use! Keywords: edm hip hop rap electronic mysterious mission hacking chase exciting spy ios ambient chilling love happy sad angry suspense
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: nathan
» Description:

A recorded sound of the Dimensional Fork Gate.