Free ActionScript

Flash AS2 & AS3 Tutorials, Game Code, Effects, Source Files & Sample Downloads

Player Movement – Mouse follow with Easing

No Comments »

Another player movement script: click and hold mouse follow with easing.

Click and hold to make the player follow the mouse:

View Code ACTIONSCRIPT
/**
 * 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;
        }
 
    }
 
}
Download Fla Sample

Download Fla Sample

Leave a Reply

You must be logged in to post a comment.