20Aug

APE Vectors, Built-In Collision Detection, User Control

No comments

This movie demonstrates the ability to move one object that collides with another object. You have control over the white ball by simply using your arrow keys. I modified the classes slightly and added some more configuration to the circle classes.

package {
	import flash.display.Sprite;
	import flash.events.Event;
	import org.cove.ape.APEngine;
	import org.cove.ape.Vector;
	import org.cove.ape.CircleParticle;
	import org.cove.ape.Group;
	import flash.events.KeyboardEvent;
	import flash.ui.Keyboard;
	import flash.text.TextField;

	public class Main extends Sprite
	{
		private	var cp:Circle = new Circle( stage.stageWidth / 2, stage.stageHeight / 2, 30 );
		private var walls:Walls = new Walls( stage.stageWidth, stage.stageHeight );
		private var ball:Circle = new Circle( stage.stageWidth / 2, stage.stageHeight / 2, 50, 0xb84777 );

		public function Main()
		{
			APEngine.init(1/4);

			// set up the default diplay container
			APEngine.container = this;
			APEngine.addMasslessForce(new Vector(0,0));

			APEngine.addGroup(cp);
			APEngine.addGroup(ball);
			APEngine.addGroup(walls);

			cp.addCollidable ( ball );
			cp.addCollidable ( walls );
			ball.addCollidable ( walls );

			// make the ball move
			ball.cp.velocity = (new Vector(Math.random() * 5, Math.random() * 5));

			stage.addEventListener(KeyboardEvent.KEY_DOWN, keyDownHandler);
            addEventListener(Event.ENTER_FRAME, run);
		}

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

		private function keyDownHandler(k:KeyboardEvent):void
		{
			if (k.keyCode == Keyboard.UP) {
				cp.cp.velocity = (new Vector(0, -10));
			} else if (k.keyCode == Keyboard.DOWN) {
				cp.cp.velocity = (new Vector(0, 10));
			} else if (k.keyCode == Keyboard.RIGHT) {
				cp.cp.velocity = (new Vector(10, 0));
			} else if (k.keyCode == Keyboard.LEFT) {
				cp.cp.velocity = (new Vector(-10, 0));
			}
		}
	}
}

The Walls class

package
{
	import org.cove.ape.Group;
	import org.cove.ape.RectangleParticle;  

	public class Walls extends Group
	{

		public function Walls(bw:Number, bh:Number)
		{
			// top
			var t:RectangleParticle = new RectangleParticle(bw/2, -50, bw, 100, 0, true);
			addParticle(t);

			// this is the bottom
			var b:RectangleParticle = new RectangleParticle(bw/2, bh + 50, bw, 100, 0, true);
			addParticle(b);

			// left
			var l:RectangleParticle = new RectangleParticle(-50, bh / 2, 100, bh, 0, true);
			addParticle(l);  

			// right
			var r:RectangleParticle = new RectangleParticle(bw + 50, bh / 2, 100, bh, 0, true);
			addParticle(r);
		}
	}
}

The Circle Class

package
{
	import org.cove.ape.Group;
	import org.cove.ape.CircleParticle;
	import org.cove.ape.Vector;

	public class Circle extends Group
	{
		public var cp:CircleParticle;

		public function Circle(x:uint, y:uint, r:Number=5, c:uint = 0xffffff)
		{
			cp = new CircleParticle(x, y, r, false, 3, .8);
			cp.setStyle(0, 0, 0, c);
			addParticle(cp);
		}
	}
}

Share/Save/Bookmark

Wednesday, August 20th, 2008 at 9:24 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.

Leave a reply