Skip to content


APE – Basketball

To play around a little more from the find of the draggable objects in APE I made a little demo of a basketball game.

Here is the code:

package {
    import flash.display.Sprite;
    import flash.events.Event;
	import flash.events.MouseEvent;

    import org.cove.ape.*;

	public class Main extends Sprite {

		private var circle:DragableCircleParticle;
		private var circle2:DragableCircleParticle;

		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,5));

			var defaultGroup:Group = new Group();

			defaultGroup.collideInternal = true;

			/* RECTANGLE PROPERTIES
				x:Number,
				y:Number,
				width:Number,
				height:Number,
				rotation:Number = 0,
				fixed:Boolean = false,
				mass:Number = 1,
				elasticity:Number = 0.3,
				friction:Number = 0
			*/
            var floor:RectangleParticle = new RectangleParticle(stage.stageWidth/2, stage.stageHeight + 48, stage.stageWidth, 100, 0, true, 10);
            defaultGroup.addParticle(floor);

			var ceil:RectangleParticle = new RectangleParticle(stage.stageWidth/2, -50, stage.stageWidth, 100, 0, true, 10);
            defaultGroup.addParticle(ceil);

			var left:RectangleParticle = new RectangleParticle(-25, stage.stageHeight/2, 50, stage.stageHeight, 0, true, 10);
            defaultGroup.addParticle(left);

			var right:RectangleParticle = new RectangleParticle(stage.stageWidth+25, stage.stageHeight/2, 50, stage.stageHeight, 0, true, 10);
            defaultGroup.addParticle(right);

			/* CIRCLE PROPERTIES
				x:Number,
				y:Number,
				radius:Number,
				fixed:Boolean = false,
				mass:Number = 1,
				elasticity:Number = 0.3,
				friction:Number = 0
			*/

			/* BASKETBALL
			------------------
			*/
           	circle = new DragableCircleParticle(350, 150, 30, false, 20, .5);
			circle.setStyle(0, 0, 0, 0x993300);
			defaultGroup.addParticle(circle);

			/* HOOP & NET
			------------------
			*/
			var hl:CircleParticle = new CircleParticle( stage.stageWidth - 180, 300, 5, true );
			defaultGroup.addParticle ( hl ); // hoop top left

			var hl2:CircleParticle = new CircleParticle( stage.stageWidth - 170, 350, 5, false );
			defaultGroup.addParticle ( hl2 ); // hoop top left

			var hl3:CircleParticle = new CircleParticle( stage.stageWidth - 145, 430, 5, false );
			defaultGroup.addParticle ( hl3 ); // hoop top left

			var hlConnector:SpringConstraint = new SpringConstraint(hl, hl2, 0.5, true, 8);
			hlConnector.setStyle(0, 0x2c2c2c, 1, 0x2c2c2c);
			defaultGroup.addConstraint(hlConnector);  

			var hl2Connector:SpringConstraint = new SpringConstraint(hl2, hl3, 0.5, true, 8);
			hl2Connector.setStyle(0, 0x2c2c2c, 1, 0x2c2c2c);
			defaultGroup.addConstraint(hl2Connector);  

			var hr:CircleParticle = new CircleParticle( stage.stageWidth - 50, 300, 5, true );
			defaultGroup.addParticle ( hr ); // hoop top right

			var hr2:CircleParticle = new CircleParticle( stage.stageWidth - 60, 350, 5, false );
			defaultGroup.addParticle ( hr2 ); // hoop top left

			var hr3:CircleParticle = new CircleParticle( stage.stageWidth - 85, 430, 5, false );
			defaultGroup.addParticle ( hr3 ); // hoop top left

			var hrConnector:SpringConstraint = new SpringConstraint(hr, hr2, 0.5, true, 8);
			hrConnector.setStyle(0, 0x2c2c2c, 1, 0x2c2c2c);
			defaultGroup.addConstraint(hrConnector);

			var hr2Connector:SpringConstraint = new SpringConstraint(hr2, hr3, 0.5, true, 8);
			hr2Connector.setStyle(0, 0x2c2c2c, 1, 0x2c2c2c);
			defaultGroup.addConstraint(hr2Connector);

			// top of hoop connector
			var thoop:SpringConstraint = new SpringConstraint(hl, hr, 4, false, 8);
			thoop.setStyle(4, 0xffffff, 4, 0xffffff);
			defaultGroup.addConstraint(thoop);

			// bottom of hoop
			var bhoop:SpringConstraint = new SpringConstraint(hl2, hr2, 0.5, false, 8);
			bhoop.setStyle(0, 0x2c2c2c, 1, 0x2c2c2c);
			defaultGroup.addConstraint(bhoop);

			var bhoop2:SpringConstraint = new SpringConstraint(hl3, hr3, 0.5, false, 8);
			bhoop2.setStyle(0, 0x2c2c2c, 1, 0x2c2c2c);
			defaultGroup.addConstraint(bhoop2);

			// backboard
			var bb:RectangleParticle = new RectangleParticle(stage.stageWidth - 20, 220, 5, 200, 0, true, 1,.01);
			defaultGroup.addParticle ( bb ); // backboard

			APEngine.addGroup(defaultGroup);
			addEventListener(Event.ENTER_FRAME, run);
		}

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

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. xx says

    You can drag the ball offstage and lose it entirely.

    You can arrange the basket to hold the ball. Oddly, it never “settles”, but instead just sits there an jitters.



Some HTML is OK

or, reply to this post via trackback.