Skip to content


APE Project – Step 2 – Adding Circle Particles

So, now that we have the ability to dynamically draw rectangles, from Part 1, I have integrated a button that will add a Circle Particle and since it is in the same group as the rectangle particle that the user draws, collision detection can be calculated. To use this demo, simply drag your mouse across the movie to create the rectangle particle and press the button to create the circle particle that will essentially fall down.

Here is the Document Class Main.as – I commented out the code from Part 1 to show some of the changes.

package {
	import flash.display.Sprite;
	import flash.display.Shape;
	import flash.events.MouseEvent;
	import flash.events.Event;
	import flash.geom.Rectangle;
	import fl.controls.Button;
	import org.cove.ape.*;

	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  

		private var dynRect:RectangleParticle
		private var circle:CircleParticle;
        private var defaultGroup:Group;  

		public function Main()
		{
			init();
		}

		private function init():void
		{
			stage.frameRate = 55;  

            // Initialize the engine. The argument here is the time step value.
            // Higher values scale the forces in the sim, making it appear to run
            // faster or slower. Lower values result in more accurate simulations.
            APEngine.init(1/4);  

            // set up the default diplay container
            APEngine.container = this;  

            APEngine.addMasslessForce(new Vector(0,8));  

            defaultGroup = new Group();  

            defaultGroup.collideInternal = true;  

			var rect:RectangleParticle = new RectangleParticle(345, 690, 710, 20, 0, true);
            defaultGroup.addParticle(rect);  

            APEngine.addGroup(defaultGroup);

			addEventListener(Event.ENTER_FRAME, run);

			stage.addEventListener(MouseEvent.MOUSE_DOWN, beginRectDraw);
			stage.addEventListener(MouseEvent.MOUSE_UP, stopRectDraw);

			// build the UI
			buildUI();
		}

		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();
			drawRectangle();
		}

		/*
		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);
		}
		*/

		private function drawRectangle():void
		{
			dynRect = new RectangleParticle(beginX + ( (endX - beginX)/2 ), beginY + ((endY - beginY)/2), endX - beginX, endY - beginY, 0, true);
            defaultGroup.addParticle(dynRect);
		}

		private function addBall( m:MouseEvent ):void
        {
            circle = new CircleParticle(Math.random() * stage.stageWidth, Math.random() * 100, Math.random() * 25 + 5);
            defaultGroup.addParticle(circle);
        }

		private function run(e:Event):void
        {
            APEngine.step();
            APEngine.paint();
        }  

		private function buildUI():void
		{
			// NEW BUTTON
            var button:Button = new Button();
            button.width = 150;
            button.height = 30;
            button.move ( stage.stageWidth / 2 - button.width / 2, stage.stageHeight - 50 - button.height / 2 );
            button.label = "Add Ball";      

            addChild(button);      

            button.addEventListener(MouseEvent.CLICK, addBall);
		}
	}
}

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.