Thanks for the collision response example.
I thought I might help by explaining what I learned while getting it
to work.
I extended the RigidBody class with the new class LoudBody based on
your RigidBody2 class. (Loud because it's dispatching a bunch of
events.)
I'll only use LoudBody objects when they need collision responses.
package {
import flash.events.Event;
import flash.events.EventDispatcher;
import flash.events.IEventDispatcher;
import flash.utils.getTimer;
import org.rje.glaze.engine.dynamics.RigidBody;
//Simple collision response code.
public class LoudBody extends RigidBody implements IEventDispatcher {
private var lastTime:uint;
private var lastBody:RigidBody;
private var dispatcher:EventDispatcher;
public function LoudBody(type:int = DYNAMIC_BODY, m:Number = -1,
i:Number = -1) {
super(type, m, i);
dispatcher = new EventDispatcher (this);
// subscribe to "onEndCollision" calls
collisionProcessingMask = 4;
}
override public function onEndCollision(body:RigidBody):void {
if ((lastBody != body) || (getTimer () - lastTime > 2000)) {
lastBody = body;
lastTime = getTimer ();
var bonk:String;
if (body.isStatic) {
// we hit obstacles: and it makes a "donk" sound
bonk = "donk";
} else {
if (v.magnitude () > body.v.magnitude ()) {
// we hit enemy: it dies
bonk = "kill";
} else {
// enemy hit us: we get hurt
bonk = "ouch";
}
}
// fire CollisionEvent event
trace("collisionType: " + bonk);
dispatchEvent(new CollisionEvent(bonk));
}
}
// IEventDispatcher methods below
public function addEventListener(type:String, listener:Function,
useCapture:Boolean = false, priority:int = 0, useWeakReference:Boolean
= false):void {
dispatcher.addEventListener(type, listener, useCapture, priority);
}
public function dispatchEvent(evt:Event):Boolean {
return dispatcher.dispatchEvent(evt);
}
public function hasEventListener(type:String):Boolean {
return dispatcher.hasEventListener(type);
}
public function removeEventListener(type:String, listener:Function,
useCapture:Boolean = false):void {
dispatcher.removeEventListener(type, listener, useCapture);
}
public function willTrigger(type:String):Boolean {
return dispatcher.willTrigger(type);
}
}
}
Here is what might appear in the the CollisionEvent class, but
(for my example the RigidBody2Collision is called CollisionEvent.)
package {
import flash.events.Event;
public class CollisionEvent extends Event {
public static const DONK:String = "donk";
public static const OUCH:String = "ouch";
public static const KILL:String = "kill";
public var message:String;
public function CollisionEvent(message:String = "CollisionEvent") {
super(message);
this.message = message;
}
}
}
Meanwhile, somewhere in the class that instantiated the LoudBody:
import LoudBody;
import CollisionEvent
.........
loudBox();
}
private function loudBox():void {
var lbSize:Number = Math.random()*15 +5;
var lboxBody:LoudBody = new LoudBody(RigidBody.DYNAMIC_BODY,lbSize,
100);
lboxBody.p.setTo(Math.random()*600,0);
var boxShape:GeometricShape = new Polygon(Polygon.createRectangle
(lbSize, lbSize), Vector2D.zeroVect, wood);
lboxBody.addShape(boxShape);
lboxBody.canSleep = true;
space.addRigidBody(lboxBody);
lboxBody.addEventListener(CollisionEvent.DONK, donkHandler);
lboxBody.addEventListener(CollisionEvent.OUCH,
ouchHandler);
lboxBody.addEventListener(CollisionEvent.KILL,
killHandler);
}
private function donkHandler(e:Event):void {
trace(e.target, "got donked");
}
private function donkHandler(e:Event):void {
trace(e.target, "got hurt");
}
private function killHandler(e:Event):void {
trace(e.target, "kilt some guy");
}
..........