Categories
Articles

Flex Asteriods Game Ship Animation

By Lon (Alonzo) Hosford

This example is based on Keith Peters ground breaking book on animation for ActionScript 3, Foundation Actionscript 3.0 Animation: Making Things Move.

Keith lays out the basic fundamentals of motion. A great start point example to understand velocity, friction, acceleration and direction is the Asteroids game style ship moving through space. I also reproduced a similar example using HTML5 canvas you can view here: HTML5 Canvas Based Animation Asteriods Space Ship Movement

Keith Peters AS3 Animation
Learn More

Many similar examples just assume you are going to use the keyboard to control the ship. This example decouples the ship acceleration and movement from keyboard. However we then double back and use the keyboard to control the ship. You will see in the MXML Application code, the keyboard events call methods on the ArrowShip class. However you could provide buttons for mouse clicks or even go crazy with a multi-touch application.

The example is created in Flex Builder Running Eclipse but can be adapted to AIR or Flash. Download the example code. You can also use Flash CS3 and CS4. You need create a Flash document in the src directory and set AsteriodsShipMovement as you document class. You also need to include the Flex 3 library, the Flex.swc file, into your Actionscript 3 settings. Tareq AlJaber article shows how that is done on page two of his article Embedding metadata with Flash. For your convenience you can download a Flash CS4 file that is set up, but still you may need to check the Actionscript 3 settings if the movie will not play because you might have a non-standard installation. Download the Flash CS4 example.

You can use any graphic for the ship. More on how to do that is noted on the comments for ArrowShip class code.

I added lots of comments in the code. The following is an overview of each code piece.

The AsteroidsShipMovement class is the main class to start the example. Line 17 is where you set the size and speed of your Flash movie.

The background is a simple shape. You can embellish to your preference. See line 34.

The ship needs a mask to define when it leaves view. That can be any size but generally is the same size as the stage or smaller. Thus your flying area can be smaller. The mask must be a rectangle. See line 38.

AsteroidsShipMovement.as

package
{
.
.
.
	import com.alh.ui.ships.ArrowShip;
	import com.alh.ui.ships.Ship;
	import flash.display.MovieClip;
	import flash.display.Shape;
	import flash.display.Sprite;
	import flash.display.StageScaleMode;
	import flash.events.KeyboardEvent;
	import flash.geom.Rectangle;
	import flash.ui.Keyboard;

	// SET GAME SIZE AND SPEED HERE
	[SWF(width=600, height = 600, frameRate = "30")]

	public class AsteriodsShipMovement extends Sprite
	{
		// Basic game background - set your preference here
		public static const backgroundColor:Number = 0x0000ff;
		public static const backgroundBorderColor:Number = 0x666666;
		public static const backgroundBorderWidth:Number = 2;

		// Create the ship
		private var arrowShip:ArrowShip;
		public function AsteriodsShipMovement()
		{
			// Set stage options
			initStage();

			// Create a background
			var backgroundRect:Shape = getRectShape(backgroundColor, backgroundBorderColor, backgroundBorderWidth, stage.stageWidth, stage.stageHeight)
			addChild(backgroundRect);

			// Create the boundaries for the arrowShip, create the arrowShip.
			var shipMaskRect:Shape = getRectShape(0x000000, 0x000000, backgroundBorderWidth, stage.stageWidth, stage.stageHeight)
			arrowShip = new ArrowShip(shipMaskRect);
			addChild(arrowShip);
			arrowShip.x = shipMaskRect.width / 2;
			arrowShip.y = shipMaskRect.height / 2;

			// Use keyboard events to control the ship
			stage.addEventListener( KeyboardEvent.KEY_DOWN, keyPressed);
			stage.addEventListener( KeyboardEvent.KEY_UP, keyReleased);
		}
		/**
		 * Set any stage options per your needs
		 * */
		private function initStage():void
		{
			stage.scaleMode = StageScaleMode.NO_SCALE;
		}
		/**
		 * Handler for KeyboardEvent.KEY_DOWN
		 * */
		private function keyPressed(evt:KeyboardEvent):void
		{
			// Either accelerate or turn the ship
			switch (evt.keyCode)
			{
				case Keyboard.UP:
					arrowShip.accelerate(Ship.START);
					break;
				case Keyboard.LEFT:
					arrowShip.turn(Ship.LEFT, Ship.START);
					break;
				case Keyboard.RIGHT:
					arrowShip.turn(Ship.RIGHT, Ship.START);
					break;
			}
		}

		/**
		 * Handler for KeyboardEvent.KEY_UP
		 * */

		private function keyReleased(evt:KeyboardEvent):void
		{
			// Either stop accelerating or stop turning the ship
			switch (evt.keyCode)
			{
				case Keyboard.UP:
					arrowShip.accelerate(Ship.STOP);
					break;
				case Keyboard.LEFT:
					arrowShip.turn(Ship.LEFT, Ship.STOP);
					break;
				case Keyboard.RIGHT:
					arrowShip.turn(Ship.RIGHT, Ship.STOP);
					break;
			}
		}
		/**
		 * Utility to draw a rectangle Shape object
		 * */
		private function getRectShape(bgColor:uint, borderColor:uint, borderSize:uint, width:uint, height:uint):Shape
		{
			var newShape:Shape = new Shape();
			newShape.graphics.beginFill(bgColor);
			newShape.graphics.lineStyle(borderSize, borderColor);
			newShape.graphics.drawRect(0, 0, width, height);
			newShape.graphics.endFill();
			return newShape;
		}

	}
}

The ArrowShip class is a subclass of the Ship class. The Ship class does all the common functions for ship objects. The ArrowShip class is basically the skin. You subclass the Ship class and add your ship image on line 11.

ArrowShip.as

.
.
.
package com.alh.ui.ships
{
	import flash.display.DisplayObject;

	public class ArrowShip extends Ship
	{

		[Embed(source="/assets/ArrowShip.png")]
		private var ShipImg:Class;
		private var a:int;
		/**
		 * Constructor
		 * param maskRect represents the container boundaries for the ship and its mask
		 * */
		public function ArrowShip(maskRect:DisplayObject) : void
		{
			super(maskRect);
			super.addShipImage(new ShipImg(),270);

		}
	}
}

The Ship class implements the animation direction, velocity, friction, and acceleration principles set out in Keith Peters’ book, Foundation Actionscript 3.0 Animation: Making Things Move!.
Instead of including Keyboard movement in the class as many animators do, the accelerate and turn methods were included. The UI or any code can call these methods to control the ship. On a more advanced level you could create an interface to further specify these methods in all types of objects that move forward and turn.

Ship.as

.
.
.
package com.alh.ui.ships
{

	import flash.display.DisplayObject;
	import flash.display.MovieClip;
	import flash.display.Sprite;
	import flash.events.Event;

	/**
	 * Generic class for a space ship moving through space
	 * This class is intended to be abstract
	 * */
	public class Ship extends MovieClip
	{

		private var _shipDirectionOffset:Number = 0; 	// Offset front of ship in artwork in degrees.
														// Artwork may have the ship facing in different
														// directions and this makes the adjustment
														// 0 = artwork ship faces right. 270 faces up.
		private var _speed:Number = 0.3;				// Acceleration increment
		private var _rotateSpeed:Number = 3;			// Turning speed in degrees
		private var _vx:Number = 0;						// Velocity for x (direction and speed)
		private var _vy:Number = 0;						// Velocity for y(direction and speed)
		private var _friction:Number = 0.95;			// Percent reduction in _vx and _vy
		private var _accelerate:Boolean = false;		// True if increasing _vx and _vy, false if not
		private var _turnLeft:Boolean = false;			// Right turn direction request
		private var _turnRight:Boolean = false;			// Object has right turn direction request

		public static const START:int = 0;				// Start moving or turning
		public static const STOP:int = 1;				// Stop moving or turning
		public static const LEFT:int = 0;				// Turn left
		public static const RIGHT:int = 1;				// Turn right

		/**
		 * Constructor
		 * param maskRect represents the container boundaries for the ship and its mask
		 * */
		public function Ship(maskRect:DisplayObject) : void
		{
			mask = maskRect;
			// Register handler for Event.ENTER_FRAME
			addEventListener(Event.ENTER_FRAME, enterFrameEventHandler, false, 0, true);
		}
		/**
		 * Add the ship image and its directional offset
		 * param imgClass is the artwork
		 * param shipDirectionOffset see _shipDirectionOffset for notes
		 * */
		internal function addShipImage(imgClass:DisplayObject, shipDirectionOffset:Number):void
		{
			_shipDirectionOffset = shipDirectionOffset;
			// Add in ship image
			var shipSprite:Sprite = new Sprite();
			shipSprite.addChild(imgClass);

			// Add ship sprite and center
			addChild(shipSprite);
			shipSprite.x = shipSprite.width / 2* -1;
			shipSprite.y = shipSprite.height / 2 * -1;

		}
		/**
		 * Accelerates ship or stops acceleration
		 * param startStopState valid values START and STOP
		 * */
		public function accelerate(startStopState:int):void
		{
			// Set the accelerating state
			_accelerate = startStopState == START;
		}
		/**
		 * Turn ship
		 * param turnDirection valid values LEFT and RIGHT
		 * param startStopState valid values START and STOP
		 * */
		public function turn(turnDirection:int, startStopState:int):void
		{
			// Set the left turn state
			if (turnDirection == LEFT)
			{
				_turnLeft = startStopState == START;
			}
			// Set the right turn state
			if (turnDirection == RIGHT)
			{
				_turnRight = startStopState == START;
			}
		}
		/**
		 * Event.ENTER_FRAME event handler
		 * */
		protected function enterFrameEventHandler(e:Event) : void
		{
			// Acceleration is on
			if (_accelerate )
			{
				// Conversion of rotation degrees (rotation + _shipDirectionOffset) and speed to velocity
				_vy += Math.sin(degreesToRadians(rotation + _shipDirectionOffset)) * _speed;
				_vx += Math.cos(degreesToRadians(rotation + _shipDirectionOffset)) * _speed;
			}
			// Acceleration is off
			else
			{
				// reduce velocity by friction
				_vy *= _friction;
				_vx *= _friction;
			}

			// Change direction right
			if (_turnRight)
			{
				rotation += _rotateSpeed;
			}
			// Change direction left
			else if (_turnLeft)
			{
				rotation += -_rotateSpeed;
			}

			// Move position by velocity
			y += _vy;
			x += _vx;

			// Exiting right side of mask
			if (x - width / 2 > mask.width)
			{
				x = 0;
			}
			// Exiting left side of mask
			else if (x + width / 2 < 0)
			{
				x = mask.width;
			}

			// Exiting top of mask
			if (y - height / 2 > mask.height)
			{
				y = 0;
			}
			// Exiting bottom of mask
			else if (y + height / 2 < 0)
			{
				y = mask.height;
			}
		}
		/**
		 * Change the default friction value
		 * */
		protected function set friction(friction:Number):void
		{
			_friction = friction;
		}
		/**
		 * Change the default speed value
		 * */
		protected function set speed(speed:Number):void
		{
			_speed = speed;
		}
		/**
		 * Change the default rotateSpeed value
		 * */
		protected function set rotateSpeed(rotateSpeed:Number):void
		{
			_rotateSpeed = rotateSpeed;
		}
		/**
		 * Utility to convert Actionscript degrees (0-360) to Trig radians (57.2958 degrees).
		 * */
		private function degreesToRadians(degrees:Number) : Number
		{
			return degrees * Math.PI / 180;
		}

	}

}

Categories
Articles

Actionscript 3 Animating Sprite Rotation Following Mouse Movement

By Lon (Alonzo) Hosford

This my own version of of Keith Peter’s Foundation Actionscript 3.0 Animation: Making Things Move chapter 3 implementation of sprite rotation following a mouse.

Download files
[August 10 2010 – I updated this to an Actionscript project in Flex Builder 4. ]

Keith Peters AS3 Animation
Learn More

You can build this with the free Flex SDK by using the code in the src folder. Same for Flash CS3 and later versions. You need to create a Flash Document in the src folder and set the document class to Chapter03_Rotation_AS3. For your convenience the Flash CS4 example download is included.

This article shows the code for the Flex project.

Application Class – Chapter03_Rotation_Flex
This Flex version is a spark implementation. The SpriteVisualElement component shown on line 51 is used to add the code to the application display on line 31.

<?xml version="1.0" encoding="utf-8"?>
<!--
	Application class for showing sprite rotation following mouse movement. 
	<p>Author: Lon Hosford https://www.lonhosford.com 908 996 3773</p>
    <p>Reference: Keith Peter's Actionscript 3.0 Animation Chapter 3</p>
	
-->
<s:Application xmlns:fx="http://ns.adobe.com/mxml/2009" 
			   xmlns:s="library://ns.adobe.com/flex/spark" 
			   xmlns:mx="library://ns.adobe.com/flex/mx" width="400" height="400"
			   creationComplete="creationCompleteHandler(event)" 
			   viewSourceURL="srcview/index.html">
	<fx:Script>
		<![CDATA[
			import mx.events.FlexEvent;
			
			// Properties for background
			private static const backgroundColor:Number = 0x0000ff;
			private static const backgroundBorderColor:Number = 0x666666;
			private static const backgroundBorderWeight:Number = 2;
			/**
			 * Handler for Application creationComplete event
			 * */
			protected function creationCompleteHandler(event:FlexEvent):void
			{
				// Create an Arrow object
				var arrow:Arrow = new Arrow();
				// Wrap arrow object into a RotateSpriteToMouse object
				var rotateToMouse:RotateSpriteToMouse = new RotateSpriteToMouse(arrow);
				// Add rotateToMouse Sprite to a SpriteVisualElement
				arrowVisualElement.addChild(rotateToMouse);
				// Center the SpriteVisualElement
				arrowVisualElement.x = background_bc.width / 2;
				arrowVisualElement.y = background_bc.height / 2;
			}

		]]>
	</fx:Script>
	<!--- 
	Background for app 
	--> 
	<s:BorderContainer id = "background_bc"
					   width="{width}" height = "{height}"
					   borderWeight="{backgroundBorderWeight}"
					   borderColor="{backgroundBorderColor}"
					   backgroundColor="{backgroundColor}">

		<!--- 
		Spark container for Sprite 
		--> 
		<s:SpriteVisualElement id="arrowVisualElement" />

	</s:BorderContainer>
					   
</s:Application>

[ad name=”Google Adsense”]
RotateSpriteToMouse Class
This is the class that does the work. Listening to the Event.ENTER_FRAME event leads to the update_rotation() method that does the work of updating the rotation.

package
{
	import flash.display.Sprite;
	import flash.events.Event;
	/**
	 * Rotates sprite to mouse position
	 * */
	public class RotateSpriteToMouse extends Sprite
	{
		private var _sprite_to_rotate:Sprite;	// Sprite to rotate to mouse
		/**
		 * Constructor 
		 * @param sprite_to_rotate The sprite to rotate
		 * */
		public function RotateSpriteToMouse(sprite_to_rotate:Sprite)
		{
			_sprite_to_rotate = sprite_to_rotate;
			addChild(_sprite_to_rotate);
			addEventListener(Event.ENTER_FRAME, enterFrameEventHandler);
		}
		/**
		 * The event handler for Event.ENTER_FRAME
		 * */
		private function enterFrameEventHandler(event:Event):void
		{
			update_rotation();
		}
		/**
		 * Updates the rotation of the _sprite_to_rotate
		 * */
		private function update_rotation():void
		{
			// Triangle adjacent angle side distance for the x value.
			var dx:Number = mouseX - _sprite_to_rotate.x;
			// Triangle opposite angle side distance for the y value.
			var dy:Number = mouseY - _sprite_to_rotate.y;
			// Compute angle in radians from the sprite to the mouse position.
			var radians:Number = Math.atan2(dy, dx);
			// Convert radians to degrees
			_sprite_to_rotate.rotation = radians * 180 / Math.PI;
		}
	}
}

Arrow Class
Simple arrow sprite that Keith wrote. Key here is the center registration point.

package
{
	import flash.display.Sprite;
	/**
	 * Creates an arrow sprite with fixed dimensions
	 * */
	public class Arrow extends Sprite
	{
		public function Arrow() 
		{
			draw();
		}
		/**
		 * Draw the arrow
		 * */
		private function draw():void
		{
			graphics.lineStyle(1, 0, 1);
			graphics.beginFill(0xffff00);
			graphics.moveTo(-50, -25);
			graphics.lineTo(0, -25);
			graphics.lineTo(0, -50);
			graphics.lineTo(50, 0);
			graphics.lineTo(0, 50);
			graphics.lineTo(0, 25);
			graphics.lineTo(-50, 25);
			graphics.lineTo(-50, -25);
			graphics.endFill();
		}
	}
	
}
Categories
Articles

Actionscript 3 Animation Following the Mouse to Show Velocity at an Angle

By Lon (Alonzo) Hosford

See also the same done in HTML5 canvas HTML5 Canvas Based Animation Rotate Arrow To Mouse Position

In chapter 5 of Keith Peter’s Foundation Actionscript 3.0 Animation: Making Things Move velocity at an angle is explained. I took Keith’s mouse following example demonstrating velocity at an angle and punched it up a small notch.

Download files
[August 10 2010 – I updated this to an Actionscript project in Flex Builder 4. ]

You can build this with the free Flex SDK by using the code in the src folder. Same for Flash CS3 and later versions. You need to create a Flash Document in the src folder and set the document class to Chapter05_FollowMouse. For your convenience the Flash CS4 example download is included.

Keith Peters AS3 Animation
Learn More

This article shows the code for the Flex project. This Flex version is a spark implementation.

The arrow will follow the mouse around the stage. It will stop following when requested and start again with a mouse click on the stage. It also waits patiently when the mouse leaves the stage. The arrow gets real frustrated when you put the mouse over it. It always points towards the mouse when the mouse is over the stage.

Application Class – Chapter04_SprayPaint
I added user mouse click interaction to control the start and stop movement of the _arrow sprite. See line 40 and lines 48 to 51.

The movement also stops and starts when the mouse enters and leaves the stage. To accomplish that the stage needed to register a Event.MOUSE_LEAVE event to detect the mouse leaving the stage. The stage object is available when the applicationComplete(...) event occurs. The MouseEvent.MOUSE_MOVE sufficed to detect the mouse back over the stage. The _mouseOverStage variable carries the state of the mouse over the stage.

The rotation of the _arrow sprite is continuous. Gives it a mouse awareness state.

<?xml version="1.0" encoding="utf-8"?>
<!--
	Application class to demonstrate animation trigonometry to show velocity at an angle. 
    Mouse position determines the angle. 
	<p>Author: Lon Hosford https://www.lonhosford.com 908 996 3773</p>
    <p>Reference: Keith Peter's Actionscript 3.0 Animation Chapter 5</p>
	
-->
<s:Application xmlns:fx="http://ns.adobe.com/mxml/2009" 
			   xmlns:s="library://ns.adobe.com/flex/spark" 
			   xmlns:mx="library://ns.adobe.com/flex/mx" width="400" height="400"
			   applicationComplete="applicationCompleteHandler(event)"
			   viewSourceURL="srcview/index.html">
	<fx:Script>
		<![CDATA[
			import mx.events.FlexEvent;
			public static const backgroundColor:Number = 0x0000ff;
			public static const backgroundBorderColor:Number = 0x666666;
			public static const backgroundBorderWeight:Number = 2;
			
			private var _instructions_tf:TextField;						// Instructions for user
			[Bindable]
			private var _lang_instructions:String = "Click to start and stop. Move mouse for animation."
			private var _arrow:Arrow;									// Animated sprite is an Arrow 
			private var _speed:Number = 5;								// Movement speed
			private var _allowMoving:Boolean = false;					// Use allowing moving state
			private var _mouseOverStage:Boolean = true;					// Mouse on the stage state
			
			/**
			 * Handler for Application applicationComplete event
			 * */
			protected function applicationCompleteHandler(event:FlexEvent):void
			{
				// Create Arrow object and add to stage. This is animated.
				_arrow = new Arrow();
				arrowVisualElement.addChild(_arrow);
				_arrow.x = (width - _arrow.width ) / 2;
				_arrow.y = (height - _arrow.height ) / 2;
				
				
				stage.addEventListener(MouseEvent.CLICK, mouseClickEventHandler);
				stage.addEventListener(MouseEvent.MOUSE_MOVE, mouseMoveEventHandler);
				stage.addEventListener(Event.ENTER_FRAME, enterFrameEventHandler);
				stage.addEventListener(Event.MOUSE_LEAVE, mouseLeaveEventHandler);
			}
			/**
			 * MouseEvent.CLICK handler
			 * */
			private function mouseClickEventHandler(event:MouseEvent):void
			{
				_allowMoving = !_allowMoving;
			}
			/**
			 * MouseEvent.MOUSE_MOVE handler
			 * */
			private function mouseMoveEventHandler(event:MouseEvent):void
			{
				_mouseOverStage = true;
			}
			/**
			 * Event.MOUSE_LEAVE handler
			 * */
			private function mouseLeaveEventHandler(event:Event):void
			{
				_mouseOverStage = false;
			}
			
			/**
			 * Event.ENTER_FRAME handler
			 * */
			private function enterFrameEventHandler(event:Event):void
			{
				draw();
			}

[ad name=”Google Adsense”]
The draw() method on line 77 updates the animation. The dx and dy variables are the distance from the mouse to the registration point of the _arrow sprite. Radians for the angle are computed using the Math.atan2(...) method. The angle in radians is converted for Actionscript rotation property to degrees. These trigonometry formulas are covered in Keith’s book .

The state the user choose to have the mouse follow is tested and if true the moveTo() method on line 98 computes the velocity at an angle discussed in chapter 5.

The _arrow sprite is designed with a center registration point to facilitate the rotation. As a result the _arrow sprite will move until the mouse coordinates reach the registration point. I added the hitTestPoint(...) method on line 105 to adjust so that the movement stops at the edge of the pixels in the _arrow sprite. The result the _arrow sprite’s point comes very close to the mouse versus the center of the _arrow sprite.

			/**
			 * Draw
			 * */
			private function draw():void
			{		
				// Distance of arrow registration point from mouse
				var dx:Number = mouseX - _arrow.x ;
				var dy:Number = mouseY - _arrow.y ;
				
				// Get angle in radians
				var angle:Number = Math.atan2(dy, dx);
				// Rotate converting radians to degrees
				_arrow.rotation = angle * 180 / Math.PI;
				
				// Is in a mouse following state
				if (_allowMoving)
				{
					// Move based on angle
					moveTo(angle);
				}
			}
			/**
			 * Move arrow
			 * */
			private function moveTo(angle:Number):void
			{
				// Velocity based on angle
				var vx:Number = Math.cos(angle) * _speed;
				var vy:Number = Math.sin(angle) * _speed;
				
				// Mouse position overlaps shape and mouse is over a pixel in the object
				if (!_arrow.hitTestPoint(mouseX, mouseY, true) && _mouseOverStage)
				{
					// Add velocity to position
					_arrow.x += vx ;
					_arrow.y += vy ;
				}
			}
			/**
			 * Set any stage options per your needs
			 * */
			private function initStage():void 
			{
				stage.scaleMode = StageScaleMode.NO_SCALE;
			}
			/**
			 * Instructions for user
			 * */
			private function getInstructions_tf():TextField 
			{
				var tf:TextField = new TextField();
				tf.autoSize = TextFieldAutoSize.LEFT;			
				tf.background = true;
				var textFormat:TextFormat = new TextFormat();
				textFormat.font = "_typewriter";
				tf.defaultTextFormat = textFormat;
				tf.text = _lang_instructions;
				return tf;
			}
		]]>
	</fx:Script>

[ad name=”Google Adsense”]
This part of the application represents the Flex UI based in Spark components.

	<!--- 
	Background for app 
	--> 
	<s:BorderContainer id = "background_bc"
					   width="{width}" height = "{height}"
					   borderWeight="{backgroundBorderWeight}"
					   borderColor="{backgroundBorderColor}"
					   backgroundColor="{backgroundColor}">

		<!--- 
		Spark container for Sprite 
		--> 
		<s:SpriteVisualElement id="arrowVisualElement" />

	</s:BorderContainer>
	<!--- 
	Instructions 
	--> 
	<s:HGroup  horizontalAlign="center" x="0" y="378" width = "100%">
		<s:Label text="{_lang_instructions}" color="0xffffff"/>
	</s:HGroup>
					   
</s:Application>

[ad name=”Google Adsense”]

Categories
Articles

Actionscript 3 Spray Paint with BitmapData

By Lon (Alonzo) Hosford

This my own version of of Keith Peter’s Foundation Actionscript 3.0 Animation: Making Things Move chapter 4 use of the BitmapData class to create a spray paint example.

Download files
[August 10 2010 – I updated this to an Actionscript project in Flex Builder 4. ]

You can build this with the free Flex SDK by using the code in the src folder. Same for Flash CS3 and later versions. You need to create a Flash Document in the src folder and set the document class to Chapter04_SprayPaint. For your convenience the Flash CS4 example download is included.

Keith Peters AS3 Animation
Learn More

This article shows the code for the Flex project. This Flex version is a spark implementation.

Application Class – Chapter04_SprayPaint
The canvas instance of the BitmapData class is instantiated line 32. The BitmapData class setPixel32(...) method on line 83 is used to set the color and alpha transparency values of a single 32 bit pixel.

The overall user interaction is mouse down start the enter frame events that call the draw() method and also to select a random color.

<?xml version="1.0" encoding="utf-8"?>
<!--
	Application class to demonstrate use of the BitmapData class along with animation trigonometry. 
	<p>Author: Lon Hosford https://www.lonhosford.com 908 996 3773</p>
    <p>Reference: Keith Peter's Actionscript 3.0 Animation Chapter 4</p>
	
-->
<s:Application xmlns:fx="http://ns.adobe.com/mxml/2009" 
			   xmlns:s="library://ns.adobe.com/flex/spark" 
			   xmlns:mx="library://ns.adobe.com/flex/mx" width="400" height="400"
			   creationComplete="creationCompleteHandler(event)" 
			   viewSourceURL="srcview/index.html">
	<fx:Script>
		<![CDATA[
			import mx.events.FlexEvent;
			
			// Properties for background
			private static const backgroundColor:Number = 0xffffff;
			private static const backgroundBorderColor:Number = 0x666666;
			private static const backgroundBorderWeight:Number = 2;
			private var canvas:BitmapData;			// Pixel level access
			private var color:uint;					// Randomly generated spray brush colors
			private var size:Number = 20;			// Size of the spray brush
			private var density:Number = 50;		// Number of pixels sprayed per frame
			/**
			 * Handler for Application creationComplete event
			 * */
			protected function creationCompleteHandler(event:FlexEvent):void
			{
				
				// Create BitmapData object allowing use to work with pixels
				canvas = new BitmapData( background_bc.width,background_bc.height, true, 0x00000000);
				var bmp:Bitmap =  new Bitmap(canvas);
				
				// Add itmapData object to a SpriteVisualElement
				arrowVisualElement.addChild(bmp);
				// Handlers for mouse events to signal drawing.
				addEventListener(MouseEvent.MOUSE_DOWN, mouseDownEventHandler);
				addEventListener(MouseEvent.MOUSE_UP, mouseUpEventHandler);
			}
			/**
			 * Event handler for MouseEvent.MOUSE_DOWN
			 * */
			private function mouseDownEventHandler(event:Event):void 
			{
				// Random color from 0 to highest color value which is white
				// and then add alpha channel of opaque
				color = Math.random() * 0xffffff + 0xff000000;
				// Start enter frame events
				stage.addEventListener(Event.ENTER_FRAME, enterFrameEventHandler);
			}
			/**
			 * Event handler for MouseEvent.MOUSE_UP
			 * */
			private function mouseUpEventHandler(event:Event):void 
			{
				stage.removeEventListener(Event.ENTER_FRAME, enterFrameEventHandler);
			}
			/**
			 * Event handler for Event.ENTER_FRAME
			 * */
			private function enterFrameEventHandler(event:Event):void 
			{
				draw();
			}

[ad name=”Google Adsense”]
The draw method uses the density variable on line 72 to determine the number of pixels to color using randomization to compute an angle and a radius within the size of the brush.

Each pixel is basically a point on its own circle. The mouse is becomes a center point for a random circle with a diameter up to the size variable. Line 77 computes the radius for that circle.

Keith’s formulas for computing points on a circle math from chapter 3 are then used to compute the position of the pixel on lines 79 and 81.


			/**
			 * Draw
			 * */
			private function draw():void 
			{
				// Repeat for number of pixels - density.
				for (var i:int = 0; i < density; i++)
				{
					// Random angle 0 - 6.2832 radians  (0 - 360)
					var angle:Number = Math.random() * Math.PI * 2;
					// Radius is random percentage of maximum radius (size / 2)
					var radius:Number = Math.random() * size / 2;
					// Center is mouseX to compute the x point
					var xpos:Number = mouseX + Math.cos(angle) * radius;
					// Center is mouseY to compute the y point
					var ypos:Number = mouseY + Math.sin(angle) * radius;
					//Set the color and alpha transparency values of a single 32 bit pixel.
					canvas.setPixel32(xpos, ypos, color);
					
				}
			}

		]]>
	</fx:Script>

[ad name=”Google Adsense”]
The SpriteVisualElement component allows adding the canvas BitmapData object to the display list.

	<!--- 
	Background for app 
	--> 
	<s:BorderContainer id = "background_bc"
					   width="{width}" height = "{height}"
					   borderWeight="{backgroundBorderWeight}"
					   borderColor="{backgroundBorderColor}"
					   backgroundColor="{backgroundColor}">

		<!--- 
		Spark container for Sprite 
		--> 
		<s:SpriteVisualElement id="arrowVisualElement" />

	</s:BorderContainer>
	<!--- 
	Instructions 
	--> 
	<s:Label x="96" y="378" text="Click and drag mouse to spray paint."/>
					   
</s:Application>
Categories
Articles

Actionscript 3 Revolving Animation Around a Center Point

By Lon (Alonzo) Hosford

I was working through chapter 3 of Keith Peter’s Foundation Actionscript 3.0 Animation: Making Things Move and came up with this Rotator class that will revolve any sprite around a given point.

Keith Peters AS3 Animation
Learn More

[August 18 2010] I updated this to an ActionScript project to Flex Builder 4. Download the example code. You can build this with the free Flex SDK by using the code in the src folder. Same for Flash CS3 and CS4. You need to create a Flash Document in the src folder and set the document class to Chapter02_Factory_PrintCenters. For your convenience you can download a Flash CS4 ready to go example.

Application Class – Animation_Circular_Rotation
This is the application class. The actual use of the Rotator class is all done on lines 81 to 86 with the _rotator instance and the Circle class circle instance. The rest of this class is dedicated to user interaction with the keyboard to start, stop and step the animation and with informational feedback using the DebugConsole class.

The circle created on line 80 is what the Rotator class instance _rotator revolves. The Rotator class constructor requires the Sprite object it will revolve and the radius as shown on line 85.

/**
 *  Purpose: Animating a rotation motion in Actionscript 3
 *  <p>Author: Lon Hosford https://www.lonhosford.com 908 996 3773</p>
 *  <p>Version history:</p>
 *  <p>	Number:			1.00.00</p>
 *  <p>	Date:			06/15/2007</p>
 *  <p>	Programmer:		Lon Hosford: https://www.lonhosford.com</p>
 *  <p>	Changes:		Original programming</p>
 * */	

package
{
	import com.lonhosford.util.debug.lite.DebugConsole;
	
	import flash.display.Shape;
	import flash.display.Sprite;
	import flash.display.StageScaleMode;
	import flash.events.Event;
	import flash.events.KeyboardEvent;
	import flash.events.TimerEvent;
	import flash.system.Capabilities;
	import flash.text.TextField;
	import flash.text.TextFormat;
	import flash.text.TextFormatAlign;
	import flash.ui.Keyboard;
	import flash.utils.Timer;
	

	[SWF(width=600, height = 770, frameRate = 30)]
	/**
	 *  Application class
	 * */	
	public class Animation_Circular_Rotation extends Sprite
	{
		private var _debugConsole:DebugConsole = DebugConsole.getInstance();
		public var maxDebugConsoleLines:Number = 500;				// Maximum lines in debug console.
																	// More line lowers performance.
		// Basic background - set your preference here
		public static const backgroundColor:Number = 0xcccccc;
		public static const backgroundBorderColor:Number = 0x666666;
		public static const backgroundBorderWidth:Number = 2;
		
		private var _rotator:Rotator;								// Rotation animator
		
		// Language
		private var _lang_clickStage:String = "Click stage to get keyboard focus.";
		private var _lang_enterKey:String = "ENTER starts and stops animation.";
		private var _lang_spacebar:String = "SPACE BAR increments animation one step. Auto repeats after delay.";
		private var _lang_debugCleared:String = "Debug window cleared every";
		private var _lang_debugClearedWhen:String = "lines when animating to maintain low memory.";
		private var _lang_traceDisabled:String = "Trace output suppressed when animation plays in Flash Control->Test Movie.";
		private var _lang_keyboardShortcuts:String = "In Flash after Control->Test Movie, select Control->Disable Keyboard Shortcuts.";
		// Space bar repeat timer
		private var _keySpacebarTimeDelay:Number = 350;					// Delay between each Keyboard.SPACE event.
		private var _keySpacebarTimer:Timer; 							// Timer for repeating Keyboard.SPACE events.

		public function Animation_Circular_Rotation()
		{
			// Set stage options
			initStage();
			// Create a background
			var backgroundRect:Shape = getRectShape(backgroundColor, backgroundBorderColor, backgroundBorderWidth, stage.stageWidth, stage.stageHeight)
			addChild(backgroundRect);
			
			// Add output monitor
			stage.addChild(_debugConsole);
			_debugConsole.width = stage.stageWidth;
			_debugConsole.height = stage.stageHeight * .25;
			_debugConsole.y = stage.stageHeight - _debugConsole.height;
			// Disable tracing to Flash External player. Tracing impairs performance.
			if ( Capabilities.playerType == "External")
			{
				_debugConsole.isTracing = false;
			}
			// Instructions
			var instruction_tf:TextField = instructionDisplay();
			addChild(instruction_tf);
			instruction_tf.x = (stage.stageWidth - instruction_tf.width) / 2;
			
			// Create sprite to rotate
			var circle:Circle = new Circle();
			
			// Create a rotator
			_rotator = new Rotator(circle, backgroundRect.width * .3);
			_rotator.x = (stage.stageWidth - _rotator.width) / 2;
			_rotator.y = instruction_tf.y + instruction_tf.height + 5;
			addChild(_rotator);
			_rotator.addEventListener(Event.ENTER_FRAME,rotatorEnterFrameEventHandler);
			
			// Use keyboard events to control the animation
			stage.addEventListener( KeyboardEvent.KEY_DOWN, keyPressed);
			stage.addEventListener( KeyboardEvent.KEY_UP, keyReleased);
			
			_keySpacebarTimer = new Timer(_keySpacebarTimeDelay,1); 
			_keySpacebarTimer.addEventListener(TimerEvent.TIMER, keySpacebarTimerEventHandler);
		}

[ad name=”Google Adsense”]


		/**
		 * Set any stage options per your needs
		 * */
		private function initStage():void 
		{ 
			stage.scaleMode = StageScaleMode.NO_SCALE;
		}
		/**
		 * Instructions display.
		 * */
		private function instructionDisplay():TextField 
		{
			var instructions_tf:TextField = new TextField();		
			var format:TextFormat = new TextFormat();
			format.font = "_typewriter";
			format.color = 0x000000;
			format.size = 12;
			format.indent = 2;
			instructions_tf.defaultTextFormat = format;
			instructions_tf.border = true;
			instructions_tf.borderColor = 0x000000;
			instructions_tf.background = true;
			instructions_tf.backgroundColor = 0xffffff;
			instructions_tf.autoSize = TextFormatAlign.LEFT
			instructions_tf.appendText(_lang_clickStage + "\n");
			instructions_tf.appendText(_lang_enterKey + "\n");
			instructions_tf.appendText(_lang_spacebar + "\n");
			instructions_tf.appendText(_lang_debugCleared + " " + maxDebugConsoleLines + " " + _lang_debugClearedWhen + "\n");
			instructions_tf.appendText(_lang_traceDisabled + "\n");
			instructions_tf.appendText(_lang_keyboardShortcuts);
			
			return instructions_tf;
		}
		/**
		 * Handler for KeyboardEvent.KEY_DOWN
		 * */
		private function keyPressed(evt:KeyboardEvent):void 
		{
			
			switch (evt.keyCode)
			{
				case Keyboard.SPACE:
					
					if (!_keySpacebarTimer.running)
					{
						_keySpacebarTimer.start();
					}	
					break;
			}
		}
		/**
		 * Handler for KeyboardEvent.KEY_UP 
		 * */
		private function keyReleased(evt:KeyboardEvent):void 
		{
			switch (evt.keyCode)
			{
				case Keyboard.SPACE:
					if (_keySpacebarTimer.running)
					{
						_keySpacebarTimer.stop();
						_keySpacebarTimer.reset();
					}
					if (_rotator.isPlaying)
					{
						_rotator.stop();
					}
					else
					{
						_rotator.step();
					}
					
					break;
				case Keyboard.ENTER:
					if (_rotator.isPlaying)
					{
						_rotator.stop();
					}
					else
					{
						_rotator.start();
					}
					break;
			}
		}
		/**
		 * Handler for TimerEvent.TIMER event. Resets the delay interval once the default delay 
		 * is reached.
		 * */
		private function keySpacebarTimerEventHandler(event:TimerEvent):void 
		{
			_rotator.start();
		}
		/**
		 * Handler for _rotator Event.ENTER_FRAME event. 
		 * */
		private function rotatorEnterFrameEventHandler(event:Event):void 
		{
			clearDebugConsoleFlashExternalPlayer();
		}
		/**
		 * Handles slower processing in Flash external player Control->Test Movie
		 * and clears debugConsole when 100 lines are reached.
		 * */
		
		private function clearDebugConsoleFlashExternalPlayer():void
		{
			if (_debugConsole.contentLineCount > maxDebugConsoleLines && _rotator.isPlaying )
			{
				_debugConsole.clear();
				
			}
		}
		/**
		 * Utility to draw a rectangle Shape object 
		 * */
		private function getRectShape(bgColor:uint, borderColor:uint, borderSize:uint, width:uint, height:uint):Shape 
		{
			var newShape:Shape = new Shape();
			newShape.graphics.beginFill(bgColor);
			newShape.graphics.lineStyle(borderSize, borderColor);
			newShape.graphics.drawRect(0, 0, width, height);
			newShape.graphics.endFill();
			return newShape;
		}
	}
}

Rotator Class
This is the class that does the rotation. The main work is done in the move() method on lines 154 – 187. Comments tell what each step accomplishes. The lines past 187 in the move() method are for information only and can be deleted. The move() method only advances the animation one step defined by the _vr variable on line 38. Increase the _vr variable will speed up the animation and decreasing slows the animation down. As an exercise you could make this a speed option via a setter method and the constructor.

The init() method on lines 74 to 94 set the sprite to animate inside a rectangular boundary.

package
{
	import com.lonhosford.util.debug.lite.DebugConsole;
	import com.lonhosford.util.debug.lite.Debugger;
	
	import flash.display.Shape;
	import flash.display.Sprite;
	import flash.events.Event;
	
	/**
	 *  Purpose: Circular rotation of a sprite around a radius
	 *  <p>Author: Lon Hosford www.lonhosford.com 908 996 3773</p>
	 *  <p>Version history:</p>
	 *  <p>	Number:			1.00.00</p>
	 *  <p>	Date:			06/15/2007</p>
	 *  <p>	Programmer:		Lon Hosford: https://www.lonhosford.com</p>
	 *  <p>	Changes:		Original programming</p>
	 * */	
	public class Rotator extends Sprite
	{
		/**
		 * A sprite object to rotate
		 * */
		private var _spriteToRotate:Sprite;
		/**
		 * Current angle in radians. 
		 * */
		private var _angle:Number = 0;  
		/**
		 * Radius of rotation. 
		 * @default 150
		 * */
		private var _radius:Number;
		/**
		 * Velocity of the angle change in radians. 
		 * @default .05
		 * */
		private var _vr:Number = .03; // Radians
		/**
		 * Center of rotation for x.
		 * */
		private var _centerX:Number;
		/**
		 * Center of rotation for y.
		 * */
		private var _centerY:Number;
		/**
		 * Coordinate x for _spriteToRotate.
		 * */
		private var _coordinateX:Number;
		/**
		 * Coordinate y for _spriteToRotate.
		 * */
		private var _coordinateY:Number;
		/**
		 * Animation is playing.
		 * */
		private var _isPlaying:Boolean = false;
		private var debugConsole:DebugConsole = DebugConsole.getInstance();
		/**
		 * Constructor
		 * @param radius see _radius
		 * */
		public function Rotator(spriteToRotate:Sprite, radius:uint = 150)
		{
			_spriteToRotate = spriteToRotate;
			_radius = radius;
			
			init();
		}

[ad name=”Google Adsense”]

		/**
		 * Initialize the class
		 * */
		private function init():void
		{
			var boundaryBorderWidth:Number = 1
			// Create a boundaries object to set container height and width 
			var boundaries:Shape = new Shape();
			boundaries.graphics.beginFill(0xcccccc, 0);
			boundaries.graphics.lineStyle(boundaryBorderWidth, 0x000000, 0);
			boundaries.graphics.drawRect(0, 0, _radius * 2 + _spriteToRotate.width , _radius * 2 + _spriteToRotate.height );
			boundaries.graphics.endFill();
			addChild(boundaries);
			
			// Compute the center of container
			_centerX = (boundaries.width - boundaryBorderWidth)/2 ;
			_centerY = (boundaries.height - boundaryBorderWidth)/2  ;
			
			// Set starting position for sprite to rotate
			
			// Add sprite
			_spriteToRotate.alpha = .5;
			addChild(_spriteToRotate);
			move();
			
			/*----------------------------------------------------------------------
			Start statistical data for information only.
			Can delete for actual implementations
			----------------------------------------------------------------------*/		
			debugConsole.write("\n");
			debugConsole.write("Rotator.init() - width:" + width);
			debugConsole.write("Rotator.init() - height:" + height);
			debugConsole.write("Rotator.init() - _angle:" + _angle);
			debugConsole.write("Rotator.init() - _radius:" + _radius);
			debugConsole.write("Rotator.init() - Math.cos(_angle):" + Math.cos(_angle));
			debugConsole.write("Rotator.init() - Math.sin(_angle):" + Math.sin(_angle));
			/*----------------------------------------------------------------------
			End statistical data for information only.
			Can delete above for actual implementations
			----------------------------------------------------------------------*/		
		}
		/**
		 * Handler for Event.ENTER_FRAME events
		 * */
		private function onEnterFrame(event:Event):void
		{
			move();
		}
		/**
		 * Get the playing status
		 * */
		public function get isPlaying():Boolean
		{
			return _isPlaying;
		}
		/**
		 * Starts the animation.
		 * */
		public function start():void
		{
			_isPlaying = true;
			addEventListener(Event.ENTER_FRAME, onEnterFrame);
		}
		/**
		 * Stops the animation.
		 * */
		public function stop():void
		{
			_isPlaying = false;
			removeEventListener(Event.ENTER_FRAME, onEnterFrame);
		}
		/**
		 * Animates one unit of velocity.
		 * */
		public function step():void
		{
			stop();
			move();
		}

[ad name=”Google Adsense”]

		/**
		 * Animates one velocity unit.
		 * <p>Performs computations for rotation and moves sprite object</p>
		 * */
		private function move():void
		{
			/*----------------------------------------------------------------------
			Sprite coordinates
			----------------------------------------------------------------------*/		
			var _coordinateX:Number = _centerX + Math.cos(_angle) * _radius;
			var _coordinateY:Number = _centerY + Math.sin(_angle) * _radius;
			/*----------------------------------------------------------------------
			Position sprite object
			----------------------------------------------------------------------*/	
			_spriteToRotate.x = _coordinateX;
			_spriteToRotate.y = _coordinateY;
			/*----------------------------------------------------------------------
			Augment the angle by the velocity
			----------------------------------------------------------------------*/		
			_angle += _vr;
			/*----------------------------------------------------------------------
			Draw triangle
			----------------------------------------------------------------------*/		
			graphics.clear();
			graphics.lineStyle(1);
			/*----------------------------------------------------------------------
			Draw hypotenuse
			----------------------------------------------------------------------*/		
			graphics.moveTo( _coordinateX, _coordinateY );
			graphics.lineTo(_centerX, _centerY);
			/*----------------------------------------------------------------------
			Draw adjacent leg
			----------------------------------------------------------------------*/		
			graphics.lineTo(_coordinateX , _centerY);
			/*----------------------------------------------------------------------
			Draw opposite leg
			----------------------------------------------------------------------*/		
			graphics.lineTo(_coordinateX, _coordinateY);
			
			/*----------------------------------------------------------------------
			Start statistical data for information only.
			Can delete for actual implementations.
			dx and dy are not used in the class.
			----------------------------------------------------------------------*/		
			debugConsole.write("\n");
			debugConsole.write("Rotator.move() - _vr:" + _vr);
			debugConsole.write("Rotator.move() - _angle:" + _angle);
			debugConsole.write("Rotator.move() - _coordinateX:" + _coordinateX);
			debugConsole.write("Rotator.move() - _coordinateY:" + _coordinateY);
			debugConsole.write("Rotator.move() - _centerX:" + _centerX);
			debugConsole.write("Rotator.move() - _centerY:" + _centerY);
			var dx:Number = _centerX - _coordinateX;
			var dy:Number = _centerY - _coordinateY;
			debugConsole.write("Rotator.move() - Length: hypotenuse:" + ( Math.sqrt(dx * dx + dy * dy)  ));
			dx = _centerX - _coordinateX;
			dy = 0;
			debugConsole.write("Rotator.move() - Length: adjacent leg:" + ( Math.sqrt(dx * dx + dy * dy)  ));
			dx = 0;
			dy = _centerY - _coordinateY;
			debugConsole.write("Rotator.move() - Length: adjacent leg:" + ( Math.sqrt(dx * dx + dy * dy)  ));
		
			/*----------------------------------------------------------------------
			End statistical data for information only.
			Can delete above for actual implementations
			----------------------------------------------------------------------*/		
			
		}
	}
}

Circle Class
This is the sprite that is revolving in the animation.

package {
	import flash.display.Sprite;
	/**
	 *  Purpose: Draw a circle in a Sprite
	 *  <p>Author: Lon Hosford www.lonhosford.com 908 996 3773</p>
	 *  <p>Version history:</p>
	 *  <p>	Number:			1.00.00</p>
	 *  <p>	Date:			06/15/2007</p>
	 *  <p>	Programmer:		Lon Hosford: https://www.lonhosford.com</p>
	 *  <p>	Changes:		Original programming</p>
	 * */	
	public class Circle extends Sprite {
		/**
		 * Radius of the circle. 
		 * @default 40	
		 * */
		private var _radius:Number;
		/**
		 * Fill color. 
		 * @default 0x000000	
		 * */
		private var _color:uint;
		/**
		 * Constructor.
		 * @param radius See _radius
		 * @param color See _color
		 * */
		public function Circle(radius:Number=40, color:uint=0x000000) 
		{
			_radius = radius;
			_color = color;
			
			init();
			
		}
		/**
		 * Initialize the class
		 * */
		public function init():void 
		{
			draw();
		}
		/**
		 * Draw class graphics
		 * */
		public function draw():void 
		{
			graphics.clear();
			graphics.beginFill(_color,alpha);
			graphics.drawCircle(0, 0, _radius);
			graphics.endFill();
		}
	}
}