Hello, I'm fairly new to Box2D, but I am experienced in ActionScript. I'm making a game in which the player (a circle) has to climb up a growing pile of boxes while still trying to avoid getting buried. Most of what I have so far works fine, except I have one problem: the jumping. Right now, to tell if the player is in a valid place to jump, I'm allowing it to jump only if it is touching another body, which needs to be lower than the player. To do this, I edited the code in b2ContactListener.as to look like this:
This code creates two variables: isColliding and collisionShape. When a body whose UserData's name is playerShape (That's the name of the player) collides with another body, isColliding becomes true and collisionShape becomes the object that it is touching. When it stops touching it, isColliding is false. Then, from the Flash file, I create a new ContactListener and say that when the up arrow key is pressed, if isColliding is true and the player's y position is less than collisionShape's y position, then the player should jump.
Most of the time this method works, but sometimes, random objects get deleted and start falling through the ground. I also continuously get this message:
TypeError: Error #1009: Cannot access a property or method of a null object reference.
at Box2D.Dynamics::b2ContactListener/Add()
at Box2D.Dynamics.Contacts::b2PolygonContact/Evaluate()
at Box2D.Dynamics.Contacts::b2Contact/Update()
at Box2D.Dynamics::b2World/SolveTOI()
at Box2D.Dynamics::b2World/Step()
at Climber_fla::MainTimeline/Update()
Thanks, I'd appreciate any advice.
Code:
package Box2D.Dynamics{
import Box2D.Collision.*;
import Box2D.Collision.Shapes.*;
import Box2D.Dynamics.Contacts.*;
import Box2D.Dynamics.*;
import Box2D.Common.Math.*;
import Box2D.Common.*;
public class b2ContactListener{
/// Called when a contact point is added. This includes the geometry
/// and the forces.
public var isColliding:Boolean = false;
public var collisionShape:b2Body;
public virtual function Add(point:b2ContactPoint):void{
if (point.shape1.GetBody().GetUserData().name == "playerShape"){
isColliding = true;
collisionShape = point.shape2.GetBody();
}
if (point.shape2.GetBody().GetUserData().name == "playerShape"){
isColliding = true;
collisionShape = point.shape1.GetBody();
}
}
/// Called when a contact point persists. This includes the geometry
/// and the forces.
public virtual function Persist(point:b2ContactPoint):void{};
/// Called when a contact point is removed. This includes the last
/// computed geometry and forces.
public virtual function Remove(point:b2ContactPoint):void{
if (point.shape1.GetBody().GetUserData().name == "playerShape" || point.shape2.GetBody().GetUserData().name == "playerShape"){
isColliding = false;
collisionShape = undefined;
}
}
/// Called after a contact point is solved.
public virtual function Result(point:b2ContactResult):void{};
};
}
import Box2D.Collision.*;
import Box2D.Collision.Shapes.*;
import Box2D.Dynamics.Contacts.*;
import Box2D.Dynamics.*;
import Box2D.Common.Math.*;
import Box2D.Common.*;
public class b2ContactListener{
/// Called when a contact point is added. This includes the geometry
/// and the forces.
public var isColliding:Boolean = false;
public var collisionShape:b2Body;
public virtual function Add(point:b2ContactPoint):void{
if (point.shape1.GetBody().GetUserData().name == "playerShape"){
isColliding = true;
collisionShape = point.shape2.GetBody();
}
if (point.shape2.GetBody().GetUserData().name == "playerShape"){
isColliding = true;
collisionShape = point.shape1.GetBody();
}
}
/// Called when a contact point persists. This includes the geometry
/// and the forces.
public virtual function Persist(point:b2ContactPoint):void{};
/// Called when a contact point is removed. This includes the last
/// computed geometry and forces.
public virtual function Remove(point:b2ContactPoint):void{
if (point.shape1.GetBody().GetUserData().name == "playerShape" || point.shape2.GetBody().GetUserData().name == "playerShape"){
isColliding = false;
collisionShape = undefined;
}
}
/// Called after a contact point is solved.
public virtual function Result(point:b2ContactResult):void{};
};
}
This code creates two variables: isColliding and collisionShape. When a body whose UserData's name is playerShape (That's the name of the player) collides with another body, isColliding becomes true and collisionShape becomes the object that it is touching. When it stops touching it, isColliding is false. Then, from the Flash file, I create a new ContactListener and say that when the up arrow key is pressed, if isColliding is true and the player's y position is less than collisionShape's y position, then the player should jump.
Most of the time this method works, but sometimes, random objects get deleted and start falling through the ground. I also continuously get this message:
TypeError: Error #1009: Cannot access a property or method of a null object reference.
at Box2D.Dynamics::b2ContactListener/Add()
at Box2D.Dynamics.Contacts::b2PolygonContact/Evaluate()
at Box2D.Dynamics.Contacts::b2Contact/Update()
at Box2D.Dynamics::b2World/SolveTOI()
at Box2D.Dynamics::b2World/Step()
at Climber_fla::MainTimeline/Update()
Thanks, I'd appreciate any advice.