Welcome To VertexTemplates.com Flash MX 2004 Tutorials Area - Keyboard Controlled Movement (Part 3) |
![]() |
|||
Flash MX 2004 Keyboard Controlled Movement (Part 3) Tutorial.
Author: Akash Goel Reprinted from www.spoono.com
This tutorial will build on the previous two parts and introduce
the principles behind screen wrapping e.g. if an item was to disappear
of the left-hand side of the screen it will appear on the right-hand
side.
// sets our variables movement = 5; ballRadius = this._width / 2; stageBounds = _root.stage.getBounds(_root); } The only new clever bit we will use is just recycle the various points of our stage as you will see in the next bit:
// continually looks for the edges of our ball ballBounds = this.getBounds(_root); // looks for the left arrow key being pressed if (Key.isDown( 37 )) { if (ballBounds.xMax <= stageBounds.xMin) { this._x = stageBounds.xMax + ballRadius; } this._x = this._x - movement; } } If it has it makes its new position the right extremity of our stage (adding the balls radius just to make the animation a bit smoother). The complete code for our ball is -
// sets our variables movement = 5; ballRadius = this._width / 2; stageBounds = _root.stage.getBounds(_root); } onClipEvent (enterFrame) { // continually looks for the edges of our ball ballBounds = this.getBounds(_root); // looks for the left arrow key being pressed if (Key.isDown( 37 )) { if (ballBounds.xMax <= stageBounds.xMin) { this._x = stageBounds.xMax + ballRadius; } this._x = this._x - movement; } // looks for the right arrow key being pressed if (Key.isDown( 39 )) { if (ballBounds.xMin >= (stageBounds.xMax - ballRadius)) { this._x = stageBounds.Xmin - ballRadius; } this._x = this._x + movement; } // looks for the up arrow key being pressed if (Key.isDown( 38 )) { if (ballBounds.yMax <= (stageBounds.yMin + ballRadius)) { this._y = stageBounds.yMax + ballRadius; } this._y = this._y - movement; } // looks for the down arrow key being pressed if (Key.isDown( 40 )) { if (ballBounds.yMin >= (stageBounds.yMax - ballRadius)) { this._y = stageBounds.yMin - ballRadius; } this._y = this._y + 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 |
||||