Skip to content


APE - Actionscript Physics Engine Revisited - Multiple Bouncing Balls

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

Posted in APE - Actionscript Physics Engine, Actionscript 3.

One Response

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

  1. Pedro said

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

    Thanks.

Some HTML is OK

(required)

(required, but never shared)

or, reply to this post via trackback.