Free ActionScript
Home
AS3 Scripts
AS2 Scripts
Resources
About
Contact
Totally rewrote my previous AS3 smooth follow and rotation script.
This version fixes all the previous issues and optimizes everything. No more “jumpy” rotation.
Click and hold to make the player smoothly rotate to and follow the mouse:
/** * Player Movement - Follow Mouse on click with Easing and smooth rotation * --------------------- * VERSION: 2.0 * DATE: 1/18/2011 * AS3 * UPDATES AND DOCUMENTATION AT: http://www.FreeActionScript.com **/ package { import flash. display . MovieClip ; import flash. events . Event ; import flash. events . MouseEvent ; public class Main extends MovieClip { // player settings private var _moveSpeedMax: Number = 1000 ; private var _rotateSpeedMax: Number = 15 ; private var _decay: Number = .98; private var _destinationX: int = 150 ; private var _destinationY: int = 150 ; // player private var _player: MovieClip ; // global private var _isActive: Boolean = false ; private var _dx: Number = 0 ; private var _dy: Number = 0 ; private var _vx: Number = 0 ; private var _vy: Number = 0 ; private var _trueRotation: Number = 0 ; /** * Constructor */ public function Main ( ) { // create player object createPlayer ( ) ; // add listeners stage . addEventListener ( Event. ENTER_FRAME , enterFrameHandler ) ; stage . addEventListener ( MouseEvent. MOUSE_UP , onMouseUpHandler ) ; stage . addEventListener ( MouseEvent. MOUSE_DOWN , onMouseDownHandler ) ; } /** * Creates player */ private function createPlayer ( ) : void { _player = new Player ( ) ; _player. x = stage . stageWidth / 2 ; _player. y = stage . stageHeight / 2 ; stage . addChild ( _player ) ; } /** * EnterFrame Handlers */ private function enterFrameHandler ( event:Event ) : void { updatePosition ( ) ; updateRotation ( ) ; } /** * Calculate Rotation */ private function updateRotation ( ) : void { // calculate rotation _dx = _player. x - _destinationX; _dy = _player. y - _destinationY; // which way to rotate var rotateTo: Number = getDegrees ( getRadians ( _dx, _dy ) ) ; // keep rotation positive, between 0 and 360 degrees if ( rotateTo & gt; _player. rotation + 180 ) rotateTo -= 360 ; if ( rotateTo & lt; _player. rotation - 180 ) rotateTo += 360 ; // ease rotation _trueRotation = ( rotateTo - _player. rotation ) / _rotateSpeedMax; // update rotation _player. rotation += _trueRotation; } /** * Calculate Position */ private function updatePosition ( ) : void { // check if mouse is down if ( _isActive ) { // update destination _destinationX = stage . mouseX ; _destinationY = stage . mouseY ; // update velocity _vx += ( _destinationX - _player. x ) / _moveSpeedMax; _vy += ( _destinationY - _player. y ) / _moveSpeedMax; } else { // when mouse is not down, update velocity half of normal speed _vx += ( _destinationX - _player. x ) / _moveSpeedMax * .25; _vy += ( _destinationY - _player. y ) / _moveSpeedMax * .25; } // apply decay (drag) _vx * = _decay; _vy * = _decay; // if close to target, slow down turn speed if ( getDistance ( _dx, _dy ) & lt; 50 ) { _trueRotation * = .5; } // update position _player. x += _vx; _player. y += _vy; } /** * Mouse DOWN handler * @param e */ private function onMouseDownHandler ( e :MouseEvent ) : void { _isActive = true ; } /** * Mouse UP handler * @param e */ private function onMouseUpHandler ( e :MouseEvent ) : void { _isActive = false ; } /** * Get distance * @param delta_x * @param delta_y * @return */ public function getDistance ( delta_x: Number , delta_y: Number ) : Number { return Math . sqrt ( ( delta_x * delta_x ) + ( delta_y * delta_y ) ) ; } /** * Get radians * @param delta_x * @param delta_y * @return */ public function getRadians ( delta_x: Number , delta_y: Number ) : Number { var r: Number = Math . atan2 ( delta_y, delta_x ) ; if ( delta_y & lt; 0 ) { r += ( 2 * Math . PI ) ; } return r; } /** * Get degrees * @param radians * @return */ public function getDegrees ( radians: Number ) : Number { return Math . floor ( radians / ( Math . PI / 180 ) ) ; } } }
Download Fla Sample
Another player movement script: click and hold mouse follow with easing.
Click and hold to make the player follow the mouse:
/** * Player Movement - Follow Mouse on click with Easing * --------------------- * VERSION: 1.0 * DATE: 9/23/2010 * AS3 * UPDATES AND DOCUMENTATION AT: http://www.FreeActionScript.com **/ package { import flash. display . MovieClip ; import flash. events . Event ; import flash. events . MouseEvent ; public class Main extends MovieClip { // player private var _player: MovieClip ; // player settings private var _speed: Number = .1; private var _speedCurrent: Number = 0 ; private var _speedMax: Number = .1; private var _acceleration: Number = .01; private var _decay: Number = .90; // other vars private var _newDestinationX: int ; private var _newDestinationY: int ; private var _destinationX: int ; private var _destinationY: int ; private var _mouseClicked: Boolean = false ; /** * Constructor */ public function Main ( ) { createPlayer ( ) ; // add listeners stage . addEventListener ( Event. ENTER_FRAME , enterFrameHandler ) ; stage . addEventListener ( MouseEvent. MOUSE_UP , onMouseUpHandler ) ; stage . addEventListener ( MouseEvent. MOUSE_DOWN , onMouseDownHandler ) ; } private function onMouseDownHandler ( e :MouseEvent ) : void { _mouseClicked = true ; } private function onMouseUpHandler ( e :MouseEvent ) : void { _mouseClicked = false ; } /** * Creates player */ private function createPlayer ( ) : void { _destinationX = stage . stageWidth / 2 ; _destinationY = stage . stageHeight / 2 ; _player = new Player ( ) ; _player. x = stage . stageWidth / 2 ; _player. y = stage . stageHeight / 2 ; stage . addChild ( _player ) ; } /** * EnterFrame Handlers */ private function enterFrameHandler ( event:Event ) : void { if ( _mouseClicked ) { _destinationX = stage . mouseX ; _destinationY = stage . mouseY ; if ( _speedCurrent < _speedMax ) { _speedCurrent += _acceleration; } } else { if ( _speedCurrent > 0.01 ) { _speedCurrent * = _decay; } else { _speedCurrent = 0 ; } } _player. x += ( _destinationX - _player. x ) * _speedCurrent; _player. y += ( _destinationY - _player. y ) * _speedCurrent; rotatePlayer ( ) ; } private function rotatePlayer ( ) : void { var radians: Number = Math . atan2 ( _destinationY - _player. y , _destinationX - _player. x ) ; var degrees: Number = radians / ( Math . PI / 180 ) + 90 ; _player. rotation = degrees; } } }
Mouse player movement example script.
Click to make the player rotate and go to the mouse (click to go):
/** * Player Movement - Go to mouse * --------------------- * VERSION: 1.0 * DATE: 9/23/2010 * AS3 * UPDATES AND DOCUMENTATION AT: http://www.FreeActionScript.com **/ package { import flash. display . MovieClip ; import flash. events . Event ; import flash. events . MouseEvent ; public class Main extends MovieClip { // player private var _player: MovieClip ; // player settings private var _playerSpeed: Number = 10 ; // other vars private var _destinationX: int ; private var _destinationY: int ; /** * Constructor */ public function Main ( ) { createPlayer ( ) ; // add listeners stage . addEventListener ( Event. ENTER_FRAME , enterFrameHandler ) ; stage . addEventListener ( MouseEvent. CLICK , mouseHandler ) ; } /** * Creates player */ private function createPlayer ( ) : void { _destinationX = stage . stageWidth / 2 ; _destinationY = stage . stageHeight / 2 ; _player = new Player ( ) ; _player. x = stage . stageWidth / 2 ; _player. y = stage . stageHeight / 2 ; stage . addChild ( _player ) ; } /** * EnterFrame Handlers */ private function enterFrameHandler ( event:Event ) : void { _player. x += ( _destinationX - _player. x ) / _playerSpeed; _player. y += ( _destinationY - _player. y ) / _playerSpeed; } /** * Mouse Handlers */ private function mouseHandler ( event:MouseEvent ) : void { _destinationX = event. stageX ; _destinationY = event. stageY ; rotatePlayer ( ) ; } private function rotatePlayer ( ) : void { var radians: Number = Math . atan2 ( _destinationY - _player. y , _destinationX - _player. x ) ; var degrees: Number = radians / ( Math . PI / 180 ) + 90 ; _player. rotation = degrees; } } }
Mouse player movement with easing AS3 code example.
Move the mouse to make the player rotate and follow the mouse:
/** * Player Movement - Follow Mouse * --------------------- * VERSION: 1.0 * DATE: 9/23/2010 * AS3 * UPDATES AND DOCUMENTATION AT: http://www.FreeActionScript.com **/ package { import flash. display . MovieClip ; import flash. events . Event ; import flash. events . MouseEvent ; public class Main extends MovieClip { // player private var _player: MovieClip ; // player settings private var _playerSpeed: Number = 10 ; // other vars private var _destinationX: int ; private var _destinationY: int ; /** * Constructor */ public function Main ( ) { createPlayer ( ) ; // add listeners stage . addEventListener ( Event. ENTER_FRAME , enterFrameHandler ) ; } /** * Creates player */ private function createPlayer ( ) : void { _destinationX = stage . stageWidth / 2 ; _destinationY = stage . stageHeight / 2 ; _player = new Player ( ) ; _player. x = stage . stageWidth / 2 ; _player. y = stage . stageHeight / 2 ; stage . addChild ( _player ) ; } /** * EnterFrame Handlers */ private function enterFrameHandler ( event:Event ) : void { _destinationX = stage . mouseX ; _destinationY = stage . mouseY ; _player. x += ( _destinationX - _player. x ) / _playerSpeed; _player. y += ( _destinationY - _player. y ) / _playerSpeed; rotatePlayer ( ) ; } private function rotatePlayer ( ) : void { var radians: Number = Math . atan2 ( _destinationY - _player. y , _destinationX - _player. x ) ; var degrees: Number = radians / ( Math . PI / 180 ) + 90 ; _player. rotation = degrees; } } }
Page 1 of 2
AS3 Effects
AS3 Gadgets
Game Examples AS2 Effects Animated Effects Dynamic Enemy AI Game Engine Game Weapons Images Interface Math Security
Effects Animated
Effects Dynamic
Enemy AI
Game Engine
Game Weapons
Images
Interface
Math
Security
Game Examples AS3 Basics Bitmap Blitting Collision Detection Enemy AI Movement Physics Space Shooter User Interface Weapon System
Basics
Bitmap Blitting
Collision Detection
Movement
Physics
Space Shooter
User Interface
Weapon System
Site Updates
718Hosting.com
Actionscript.org Forums
Flash Game Blogs
Flash Game License
Flashkit Forums
gotoAndLearn
IndieGames.com
Kirupa’s Tutorials
Kongregate Forums
MochiAds Resources
Philip Radvan
Photon Storm
Polygonal
Radvan Media
Tonypa’s Tutorials
Register
Log in
Entries RSS
Comments RSS
WordPress.org