所以我遇到了一个我真的不明白的问题。在试图弄清楚什么是错的之后,我决定录制一个video并在这里问一下。
在视频中,请注意左下角的True / False布尔值。这是我的变量canJump的值。在开始时,只需左右移动“播放器”,值就会在true和false之间。当玩家上坡和下坡时也会发生这种情况。
使用Tiled创建地图/碰撞图层。
我的TiledObject类:
public class TiledObjectUtil {
public static float PPM = 32;
public static void parseTiledObjectLayer(World world, MapObjects objects) {
for(MapObject object : objects) {
Shape shape;
if(object instanceof PolylineMapObject) {
shape = createPolyLine((PolylineMapObject) object);
} else {
continue;
}
Body body;
BodyDef bdef = new BodyDef();
bdef.type = BodyDef.BodyType.StaticBody;
body = world.createBody(bdef);
body.createFixture(shape, 1.0f);
shape.dispose();
}
}
private static ChainShape createPolyLine(PolylineMapObject polyline) {
float[] vertices = polyline.getPolyline().getTransformedVertices();
Vector2[] worldVertices = new Vector2[vertices.length / 2];
for(int i = 0; i<worldVertices.length; i++) {
worldVertices[i] = new Vector2(vertices[i * 2] / PPM, vertices[i*2+1] / PPM);
}
ChainShape cs = new ChainShape();
cs.createChain(worldVertices);
return cs;
}}
我的玩家类:
public class Player {
private BodyDef def = new BodyDef();
public Body playerBody;
private float speed = 10;
public Player() {
}
public void update() {
if (InputUtil.moveLeft) {
playerBody.setLinearVelocity(-speed, playerBody.getLinearVelocity().y);
}
if (InputUtil.moveRight) {
playerBody.setLinearVelocity(speed, playerBody.getLinearVelocity().y);
}
if (!InputUtil.moveLeft && !InputUtil.moveRight) {
playerBody.setLinearVelocity(0, playerBody.getLinearVelocity().y);
}
}
public void jump() {
if (ContactUtil.canJump) {
playerBody.applyLinearImpulse(0, 80, 0, 0, true);
}
}
public Body createPlayer(World world) {
def.type = BodyDef.BodyType.DynamicBody;
def.position.set(20, 20);
def.fixedRotation = true;
playerBody = world.createBody(def);
PolygonShape shape = new PolygonShape();
shape.setAsBox(2f / 2, 2f / 2);
FixtureDef playerFixture = new FixtureDef();
playerFixture.density = 1f;
playerFixture.shape = shape;
playerFixture.restitution = 0f;
playerFixture.friction = 1f;
playerBody.createFixture(playerFixture);
shape.setAsBox(2f / 2, 1f / 2, new Vector2(0, 0 - 1), 0);
playerFixture.shape = shape;
playerFixture.isSensor = true;
playerBody.createFixture(playerFixture).setUserData("player");
shape.dispose();
return playerBody;
} }
最后,我的ContactListener:
public class ContactUtil implements ContactListener {
public static boolean canJump;
public ContactUtil() {
}
@Override
public void beginContact(Contact contact) {
Fixture fixtureA = contact.getFixtureA();
Fixture fixtureB = contact.getFixtureB();
System.out.println(fixtureA.getUserData() + ", " + fixtureB.getUserData());
if (fixtureA.getUserData() == "player" && fixtureB.getUserData() == null) {
canJump = true;
}
if (fixtureA.getUserData() == null && fixtureB.getUserData() == "player") {
canJump = true;
}
}
@Override
public void endContact(Contact contact) {
Fixture fixtureA = contact.getFixtureA();
Fixture fixtureB = contact.getFixtureB();
System.out.println(fixtureA.getUserData() + ", " + fixtureB.getUserData());
if (fixtureA.getUserData() == "player" && fixtureB.getUserData() == null) {
canJump = false;
}
if (fixtureA.getUserData() == null && fixtureB.getUserData() == "player") {
canJump = false;
}
}
另外,我的玩家每次在斜坡上停下来都会跳一点。我知道这是由于这一行:playerBody.setLinearVelocity(0, playerBody.getLinearVelocity().y);
如果有人知道更好的方式来处理这个动作,那将非常感激。
答案 0 :(得分:1)
我似乎记得过去有过这个问题!我认为问题在于beginContact
和endContact
中的逻辑。
我将打破我粗糙的MS Paint技能来解释这个问题。想象一下以下情况:你的玩家在空中,朝着下面的平台坠落。正如您所料,canJump
是错误的:
玩家跌落并降落,并且调用beginContact
平台1。这会将canJump
设置为true
。再次,这是我们所期望的:
现在玩家向右移动,直到他们接触到平台2。调用平台2的beginContact
。然而,canJump
被设置为真。
这就是问题所在。在最后一步,玩家仍然与平台一接触。但现在他们不是,所以调用平台一endContact
。 canJump
现已设置为false
。这解释了您获得的意外行为。
解决方案非常简单。您需要维护播放器底部正在触摸的联系人列表。在这篇iforce2d文章中有一个指南可以做到这一点:http://www.iforce2d.net/b2dtut/jumpability
祝你好运!