Free ActionScript

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

Player movement – follow mouse

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

8-way keyboard movement with rotation & velocity

This is an optimized AS3 version of the old movement code.

AS3 8-way keyboard player movement with rotation and velocity script.

Click and use Arrow Keys to move:

View Code ACTIONSCRIPT
/**
 * Player Movement - 8-way keyboard with velocity
 * ---------------------
 * 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.KeyboardEvent;
    import flash.ui.Keyboard;
 
    public class Main extends MovieClip
    {        
        // player
        private var _player:MovieClip;
 
        // player settings
        private var _maxSpeed:Number = 5;
        private var _rotationSpeed:Number = 5;
        private var _thrust:Number = .5;
        private var _decay:Number = .97;        
 
        private var _currentSpeed:Number = 0;
        private var _speedX:Number = 0;
        private var _speedY:Number = 0;
 
        // movement flags
        private var _movingUp:Boolean = false;
        private var _movingLeft:Boolean = false;
        private var _movingRight:Boolean = false;
 
        /**
         * Constructor
         */
        public function Main() 
        {
            createPlayer();
 
            // add listeners
            stage.addEventListener(Event.ENTER_FRAME, enterFrameHandler);            
            stage.addEventListener(KeyboardEvent.KEY_DOWN, myOnPress);
            stage.addEventListener(KeyboardEvent.KEY_UP, myOnRelease);            
        }
 
        /**
         * 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
        {
            // Rotate right or left
            if (_movingRight)
            {
                _player.rotation += _rotationSpeed;
            }
            if (_movingLeft)
            {
                _player.rotation -= _rotationSpeed;
            }
            if (_movingUp)
            {
                _speedX += _thrust * Math.sin(_player.rotation * (Math.PI / 180));
                _speedY += _thrust * Math.cos(_player.rotation * (Math.PI / 180));
            } 
            else{
                // Deccelerate when Up Arrow key is released
                _speedX *= _decay;
                _speedY *= _decay;
            }
 
            // Maintain speed limit
            _currentSpeed = Math.sqrt((_speedX * _speedX) + (_speedY * _speedY));
 
            if (_currentSpeed > _maxSpeed)
            {
                _speedX *= _maxSpeed/_currentSpeed;
                _speedY *= _maxSpeed/_currentSpeed;
            }
 
            // Move _player based on calculations above
            _player.y -= _speedY;
            _player.x += _speedX;
        }
 
        /**
         * Key Press Handlers
         */
        public function myOnPress(event:KeyboardEvent):void
        {
            switch( event.keyCode )
            {
                case Keyboard.UP:
                    _movingUp = true;
                    break;
 
                case Keyboard.LEFT:
                    _movingLeft = true;
                    break;
 
                case Keyboard.RIGHT:
                    _movingRight = true;
                    break;
            }
        }
 
        /**
         * Key Release Handlers
         */
        public function myOnRelease(event:KeyboardEvent):void
        {
            switch( event.keyCode )
            {
                case Keyboard.UP:
                    _movingUp = false;
                    break;
 
                case Keyboard.LEFT:
                    _movingLeft = false;
                    break;
 
                case Keyboard.RIGHT:
                    _movingRight = false;
                    break;
            }
        }
 
    }
 
}
Download Fla Sample

Download Fla Sample

8-way keyboard player movement

This is an optimized AS3 version of the old movement code.

8-way keyboard player movement script for flash games.

Click and use Arrow Keys to move:

View Code ACTIONSCRIPT
/**
 * Player Movement - 8-way keyboard
 * ---------------------
 * 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.KeyboardEvent;
	import flash.ui.Keyboard;
 
	public class Main extends MovieClip
	{		
		// player
		private var _player:MovieClip;
 
		// player settings
		private var _playerSpeed:Number = 4;
 
		// movement flags
		private var _movingUp:Boolean = false;
		private var _movingDown:Boolean = false;
		private var _movingLeft:Boolean = false;
		private var _movingRight:Boolean = false;
 
		/**
		 * Constructor
		 */
		public function Main() 
		{
			createPlayer();
 
			// add listeners
			stage.addEventListener(Event.ENTER_FRAME, enterFrameHandler);			
			stage.addEventListener(KeyboardEvent.KEY_DOWN, myOnPress);
			stage.addEventListener(KeyboardEvent.KEY_UP, myOnRelease);			
		}
 
		/**
		 * 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
		{
			// Move up, down, left, or right
			if ( _movingLeft && !_movingRight )
			{
				_player.x -= _playerSpeed;
				_player.rotation = 270;
			}
			if ( _movingRight && !_movingLeft )
			{
				_player.x += _playerSpeed;
				_player.rotation = 90;
			}
			if ( _movingUp && !_movingDown )
			{
				_player.y -= _playerSpeed;
				_player.rotation = 0;
			}
			if ( _movingDown && !_movingUp )
			{
				_player.y += _playerSpeed;
				_player.rotation = 180;
			}
 
			// Move diagonally
			if ( _movingLeft && _movingUp && !_movingRight && !_movingDown )
			{
				_player.rotation = 315;
			}
			if ( _movingRight && _movingUp && !_movingLeft && !_movingDown )
			{
				_player.rotation = 45;
			}
			if ( _movingLeft && _movingDown && !_movingRight && !_movingUp )
			{
				_player.rotation = 225;
			}
			if ( _movingRight && _movingDown && !_movingLeft && !_movingUp )
			{
				_player.rotation = 135;
			}
		}
 
		/**
		 * Key Press Handlers
		 */
		public function myOnPress(event:KeyboardEvent):void
		{
			switch( event.keyCode )
			{
				case Keyboard.UP:
					_movingUp = true;
					break;
 
				case Keyboard.DOWN:
					_movingDown = true;
					break;
 
				case Keyboard.LEFT:
					_movingLeft = true;
					break;
 
				case Keyboard.RIGHT:
					_movingRight = true;
					break;
			}
		}
 
		/**
		 * Key Release Handlers
		 */
		public function myOnRelease(event:KeyboardEvent):void
		{
			switch( event.keyCode )
			{
				case Keyboard.UP:
					_movingUp = false;
					break;
 
				case Keyboard.DOWN:
					_movingDown = false;
					break;
 
				case Keyboard.LEFT:
					_movingLeft = false;
					break;
 
				case Keyboard.RIGHT:
					_movingRight = false;
					break;
			}
		}
 
	}
 
}
Download Fla Sample

Download Fla Sample

Realistic Floating Soap Bubbles

Here is a script that I needed for work: a realistic floating soap bubbles effect in flash.

Inside Bubbles.as you can set the boundaries (min x, min y, max x and max y), the number of bubbles, and the start point/end point of where the bubbles come out from.

Download Fla Sample

Download Fla Sample