Skip to content


APE Project – Step 1 – Dynamically Create Rectangles

My thought for this project is to allow the user to dynamically create objects to be used with the Actionscript Physics Engine. The first object to create would be the rectangle. If you drag your mouse across the movie below a rectangle will be dynamically drawn based on your beginning and ending mouse positions.

package {
	import flash.display.Sprite;
	import flash.display.Shape;
	import flash.events.MouseEvent;
	import flash.geom.Rectangle;  

	public class Main extends Sprite {

		private var beginX:Number; // top left x position
		private var beginY:Number; // top left y position
       	private var endX:Number; // bottom right x position
       	private var endY:Number; // bottom right y position  

		public function Main(){
			stage.addEventListener(MouseEvent.MOUSE_DOWN, beginRectDraw);
			stage.addEventListener(MouseEvent.MOUSE_UP, stopRectDraw);
		}

		private function beginRectDraw(m:MouseEvent):void
       	{
			beginX = mouseX;
			beginY = mouseY;
       	}

		private function stopRectDraw(m:MouseEvent):void
       	{
			// set the end points
           	endX = mouseX;
		   	endY = mouseY;

			// remove the listeners
			stage.addEventListener(MouseEvent.MOUSE_DOWN, beginRectDraw);
			stage.addEventListener(MouseEvent.MOUSE_UP, stopRectDraw);

			// now display the highlighted area
			showHighlightedArea();
		}

		private function showHighlightedArea():void
		{
			var highlightArea:Shape = new Shape();  

            highlightArea.graphics.beginFill(Math.random() * 0xffffff);
            highlightArea.graphics.lineStyle(0);
            highlightArea.graphics.drawRect(beginX, beginY, endX - beginX, endY - beginY);
            highlightArea.graphics.endFill();  

            var myRectSp:Sprite = new Sprite();
            myRectSp.addChild(highlightArea);  

            addChild(myRectSp);
		}
	}
}

Posted in APE - Actionscript Physics Engine, Actionscript 3.


0 Responses

Stay in touch with the conversation, subscribe to the RSS feed for comments on this post.



Some HTML is OK

or, reply to this post via trackback.