Hey, I'm working on a side scroller game using box 2D and have run into a snag. I'm trying to get my character to jump, and i've tried a bunch of different methods including contact listeners, timers, checking the velocity, and a sensor movie clip. The sensor movie clip is what i'm currently trying to get working as I think it will be the most solid method. I've got a compound object. One Box with a smaller box at it's base that is a sensor. I'm trying to detect if just the sensor shape at the base of my box is touching anything. It's not detecting any contacts on the sensor object though. If I make the object not a sensor it detects collisions, but i'm having a hard time figuring out which contact is the sensor shape, and it doesn't always report the collisions accurately, if for example i'm standing on a dynamic box. I'll post my code here and hopefully someone can make heads or tails of what i've described.
Here is my contact listener
and this is my function for detecting if the sensor is colliding with the floor
this is my actual jump code. PlayerBody is a compound body with a box that is my players collision, and a smaller box at the base that is the sensor movie clip.
Any help would be awesome. If anyone has any questions, just ask. I've been beating my head on this for a while! Thanks!
Edit: Oh yea, I forgot to say that i'm using box 2D 2.0.1
Here is my contact listener
Code:
public class BContactListener extends b2ContactListener
{
private var cPoint:b2ContactPoint;
private var cState:String;
public override function Add(point:b2ContactPoint):void {
cPoint = point;
cState = "add";
};
public override function Persist(point:b2ContactPoint):void {
cPoint = point;
cState = "persist";
};
public override function Remove(point:b2ContactPoint):void {
cPoint = point;
cState = "remove";
};
public function returnPoint():b2ContactPoint{
return(cPoint);
};
public function returnState():String{
return(cState);
};
}
{
private var cPoint:b2ContactPoint;
private var cState:String;
public override function Add(point:b2ContactPoint):void {
cPoint = point;
cState = "add";
};
public override function Persist(point:b2ContactPoint):void {
cPoint = point;
cState = "persist";
};
public override function Remove(point:b2ContactPoint):void {
cPoint = point;
cState = "remove";
};
public function returnPoint():b2ContactPoint{
return(cPoint);
};
public function returnState():String{
return(cState);
};
}
and this is my function for detecting if the sensor is colliding with the floor
Code:
public function isResting(shape:b2Shape)
{
cPoint = level.myContactListener.returnPoint();
if (cPoint)
{
if(cPoint.shape1 == shape)
{
return true;
}
else if(cPoint.shape2 == shape)
{
return true;
}
}
}
{
cPoint = level.myContactListener.returnPoint();
if (cPoint)
{
if(cPoint.shape1 == shape)
{
return true;
}
else if(cPoint.shape2 == shape)
{
return true;
}
}
}
this is my actual jump code. PlayerBody is a compound body with a box that is my players collision, and a smaller box at the base that is the sensor movie clip.
Code:
var jumpSensorClip = PlayerBody.GetShapeList();
if (Input.isKeyPressed(controlJump))
{
if (landed == true)
{
landed = false;
PlayerBody.m_linearVelocity.y = 0;
PlayerBody.ApplyImpulse(Jump,PlayerVec);
}
}
if (isResting(jumpSensorClip))
{
landed = true;
}
if (Input.isKeyPressed(controlJump))
{
if (landed == true)
{
landed = false;
PlayerBody.m_linearVelocity.y = 0;
PlayerBody.ApplyImpulse(Jump,PlayerVec);
}
}
if (isResting(jumpSensorClip))
{
landed = true;
}
Any help would be awesome. If anyone has any questions, just ask. I've been beating my head on this for a while! Thanks!
Edit: Oh yea, I forgot to say that i'm using box 2D 2.0.1