31Jul

APE - Actionscript Physics Engine Revisited - Multiple Bouncing Balls

1 comment so far

It has been a while since I have been able to play around with APE, so I just came up with this little movie to become reacquainted with the code. To play, click the button at the bottom of the movie within the white area.

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

	public class APEMultipleCircles extends Sprite {

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

		public function APEMultipleCircles()
		{
			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(350, 600, 700, 200, 0, true);
            defaultGroup.addParticle(rect);

           	APEngine.addGroup(defaultGroup);

			addEventListener(Event.ENTER_FRAME, run);

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

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

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

Share/Save/Bookmark

Thursday, July 31st, 2008 at 9:33 am and is filed under APE - Actionscript Physics Engine, Actionscript 3. You can follow any responses to this entry through the RSS 2.0 feed. You can leave a response, or trackback from your own site.

One Response to “APE - Actionscript Physics Engine Revisited - Multiple Bouncing Balls”

  1. Posted by Pedro 17th September, 2008 at 4:39 pm

    This looks really cool, but I think you need to have sides, so the balls don’t fall out and a clear button.

    Thanks.

Leave a reply