Hi,
I am trying to customise how my b2body's react to collisions.
I found this tutorial: http://blog.allanbishop.com/box2d-2-1a-tutorial-–-part-4-collision-detection/ about over riding the box2d collision handler.
It is basically a new class which extends the b2ContactListener class. It seems to work perfectly except that I need to run this on multiple objects.
When I call the customEventListener it registers a collision but them seems to affect all of the objects I attached the listener to rather than the object that is having the collision.
Any ideas?
Here is how I add the listener (this is added every time I create a new object
Here is my listener class
What I want this code to do is only call a response on the b2body that has had a collision rather than on all of them...
Any ideas?
Thanks in advance
I am trying to customise how my b2body's react to collisions.
I found this tutorial: http://blog.allanbishop.com/box2d-2-1a-tutorial-–-part-4-collision-detection/ about over riding the box2d collision handler.
It is basically a new class which extends the b2ContactListener class. It seems to work perfectly except that I need to run this on multiple objects.
When I call the customEventListener it registers a collision but them seems to affect all of the objects I attached the listener to rather than the object that is having the collision.
Any ideas?
Here is how I add the listener (this is added every time I create a new object
Code:
plateContactListener = new game_plateContactListener();
plateContactListener.eventDispatcher.addEventListener(game_plateContactListener.SPROUT_START_CONTACT, onSproutHitPlate);
world.SetContactListener(plateContactListener);
plateContactListener.eventDispatcher.addEventListener(game_plateContactListener.SPROUT_START_CONTACT, onSproutHitPlate);
world.SetContactListener(plateContactListener);
Here is my listener class
Code:
public class game_plateContactListener extends b2ContactListener
{
public var eventDispatcher:EventDispatcher;
public static const SPROUT_START_CONTACT:String = "sproutStartContact";
public function game_plateContactListener():void {
eventDispatcher = new EventDispatcher();
}
override public function BeginContact(contact:b2Contact):void {
if ((contact.GetFixtureA().GetBody().GetUserData().type == doc_class.BRUSSEL_SPROUT && contact.GetFixtureB().GetBody().GetUserData().type == doc_class.PLATE_SENSOR) || (contact.GetFixtureA().GetBody().GetUserData().type == doc_class.PLATE_SENSOR && contact.GetFixtureB().GetBody().GetUserData().type == doc_class.BRUSSEL_SPROUT)) {
eventDispatcher.dispatchEvent(new Event(SPROUT_START_CONTACT));
}
}
}
{
public var eventDispatcher:EventDispatcher;
public static const SPROUT_START_CONTACT:String = "sproutStartContact";
public function game_plateContactListener():void {
eventDispatcher = new EventDispatcher();
}
override public function BeginContact(contact:b2Contact):void {
if ((contact.GetFixtureA().GetBody().GetUserData().type == doc_class.BRUSSEL_SPROUT && contact.GetFixtureB().GetBody().GetUserData().type == doc_class.PLATE_SENSOR) || (contact.GetFixtureA().GetBody().GetUserData().type == doc_class.PLATE_SENSOR && contact.GetFixtureB().GetBody().GetUserData().type == doc_class.BRUSSEL_SPROUT)) {
eventDispatcher.dispatchEvent(new Event(SPROUT_START_CONTACT));
}
}
}
What I want this code to do is only call a response on the b2body that has had a collision rather than on all of them...
Any ideas?
Thanks in advance