Free ActionScript

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

Player movement – follow mouse

4 Comments »

Mouse player movement with easing AS3 code example.

Move the mouse to make the player rotate and follow the mouse:

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

Download Fla Sample

4 Responses

Hi
First of all this is the greatest site ever! I am a total newbie and I found this code and it is exactly what I was looking for. I tried to recreate this in so many ways, but mine don’t seem to work and I don’t know why. I even went to create a new .fla and .as named Main2, my symbol was named Player just like the example and I kept the code the same except for changing Main to Main2. Do you know why this is not working? Is there a way for me to upload my docs so you can see?
Thanks

  • I made this in Actionscript2.
    It’s the same thing but in about 15 lines of code:

    var xDist:Number;
    var yDist:Number;
    var xRatio:Number;
    var yRatio:Number;
    ball.onEnterFrame = function() {
    xDist = _xmouse-this._x;
    yDist = _ymouse-this._y;
    this._x += xDist/40;
    this._y += yDist/40;
    disx = _root._xmouse-this._x;
    disy = _root._ymouse-this._y;
    Radians = Math.atan2(disy, disx);
    Degrees = 360*Radians/(2*Math.PI);
    this._rotation = Degrees;
    };
    make a movieclip with instance name ball and put the code on the timeline. :D
    I also made one where the speed is constant not easing, its 54 lines though.

  • hi, im trying to use this code to control the second player of a race car game im making for school, the first player is controlled by the arrow keys and i wanted player 2 to be controlled using this method. ive tried the code in various sections of my project and i havent been able to get it to work. if you have any advice maybe as far as what i should do with….idk instance names and such to get this to work. please help me soon im on a time schedule.

  • Leave a Reply

    You must be logged in to post a comment.