Free ActionScript

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

FreeActionScript’s New Look

Updated WordPress to the newest version, along with a new look. Hope you like it!

This also fixed issues with comments. You can now use breaks in your comments, as well as code tags to wrap your code.

Note: If you’re having issues with the site (if it looks broken), please clear your browser’s cache.

Draw Box Class

By request, here is a simple box drawing class. Click and drag to draw box based on mouse position.

This can be used as an object selecting mechanism in games to select multiple objects at the same time.

Click and drag to draw box

View Code ACTIONSCRIPT
/**
 * Draw Box class
 * ---------------------
 * VERSION: 1.0
 * DATE: 11/14/2010
 * AS3
 * UPDATES AND DOCUMENTATION AT: http://www.FreeActionScript.com
 **/
package com.freeactionscript
{
	import flash.display.DisplayObjectContainer;
	import flash.events.MouseEvent;
	import flash.display.Sprite;
	import flash.events.Event;
 
	public class Box extends Sprite
	{
		private var _canvas:DisplayObjectContainer;
		private var _startX:Number;
		private var _startY:Number;
		private var _endX:Number;
		private var _endY:Number;
 
		public function Box($canvas:DisplayObjectContainer, $startX:Number, $startY:Number)
		{			
			_canvas = $canvas;
			_startX = $startX;
			_startY = $startY;
			_endX = _canvas.mouseX;
			_endY = _canvas.mouseY;
 
			_canvas.addEventListener(MouseEvent.MOUSE_MOVE, onMouseMoveHandler);
			_canvas.addEventListener(MouseEvent.MOUSE_UP, onMouseUpHandler);
			_canvas.addEventListener(Event.ENTER_FRAME, onEnterFrameHandler);
		}
 
		private function onMouseMoveHandler(event:MouseEvent):void
		{
			_endX = _canvas.mouseX;
			_endY = _canvas.mouseY;
		}
 
		private function onMouseUpHandler(event:MouseEvent):void 
		{
			_canvas.removeEventListener(MouseEvent.MOUSE_MOVE, onMouseMoveHandler);			
			_canvas.removeEventListener(MouseEvent.MOUSE_UP, onMouseUpHandler);
			_canvas.removeEventListener(Event.ENTER_FRAME, onEnterFrameHandler);
		}
 
		private function onEnterFrameHandler(event:Event):void 
		{
			graphics.clear();
			graphics.lineStyle(2, 0x88B1CC);
			graphics.moveTo(_startX, _startY);
			graphics.beginFill(0x88B1CC, .25);
			graphics.lineTo(_endX, _startY);
			graphics.lineTo(_endX, _endY);
			graphics.lineTo(_startX, _endY);
			graphics.lineTo(_startX, _startY);
			graphics.endFill();
		}
	}
}
Download Fla Sample

Download Fla Sample

Player Movement – Mouse follow with Easing

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

Player movement – click to move

Mouse player movement example script.

Click to make the player rotate and go to the mouse (click to go):

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

Download Fla Sample