Welcome To VertexTemplates.com Flash MX 2004 Tutorials Area - Keyboard Controlled Movement (Part 1) |
![]() |
|||
Flash MX 2004 Keyboard Controlled Movement (Part 1) Tutorial.
Author: Akash Goel Reprinted from www.spoono.com Introduction
onClipEvent (load) { movement = 5; } onClipEvent (enterFrame) { if (Key.isDown( 37 )) { this._x = this._x - movement; } } Test your movie. When you click on your movie and use the left arrow key your object will move across the screen. So how does it work? onClipEvent (load) { movement = 5; } As described above this sets our variables - in this case movement. onClipEvent (enterFrame) { if (Key.isDown( 37 )) { this._x = this._x - movement; } } This is a loop that checks every frame if a certain key has been pressed (we have given the key code for the left arrow key). If it has it addresses the movie clip and adds our variable movement to its x coordinate. Now we have got the basics complete the code: onClipEvent (load) { movement = 5; } onClipEvent (enterFrame) { if (Key.isDown( 37 )) { this._x = this._x - movement; } if (Key.isDown( 39 )) { this._x = this._x + movement; } if (Key.isDown( 38 )) { this._y = this._y - movement; } if (Key.isDown( 40 )) { this._y = this._y + movement; } } Now test your movie. You will find your movie clip moves in the same direction as the arrow key you press. In essence the code is continually searching for what to do next and responding, by moving the movie clip across the x and y axis, to your key presses by way of their individual key codes. The Key Codes Although a full list of all the key codes is available in the Actionscript Reference Help Files for the sake of easy reference I will list the commonly used keys and their codes Left Arrow 37 Up Arrow 38 Right Arrow 39 Down Arrow 40 Space Bar 32 What Next Play about with the movement variable and the frame rate of your movie to see how these two parameters can effect your movies. Learn how to keep your movie clip on the screen in keyboard controlled movement (part 2). Click here to Go on to the next Part of Keyboard Controlled Movement> |
||||
| Home Web Templates Affiliate Program Template Tour Custom Website Design Tutorials Articles Contact Us FAQ's All Rights Reserved © 2005 VertexTemplates.com Read our Terms Of Use and Privacy Policy Flash Templates by Metamorphosis Design |
||||