Java LibGDX/Box2D车身接收到错误的y坐标
是的,我现在正在使用libGDX/Box2D,我完全被我在学习这套教程时发现的一个bug难住了 在我的游戏中,我创建了一个演员,并将其添加到我的游戏阶段。演员以给定的y坐标放置在屏幕上。游戏只是通过动画循环来模拟跑步 问题是,当我把演员放在游戏舞台上时,它的y坐标不知何故被赋予了错误的y坐标,使用下面的代码片段检索Java LibGDX/Box2D车身接收到错误的y坐标,java,libgdx,box2d,Java,Libgdx,Box2d,是的,我现在正在使用libGDX/Box2D,我完全被我在学习这套教程时发现的一个bug难住了 在我的游戏中,我创建了一个演员,并将其添加到我的游戏阶段。演员以给定的y坐标放置在屏幕上。游戏只是通过动画循环来模拟跑步 问题是,当我把演员放在游戏舞台上时,它的y坐标不知何故被赋予了错误的y坐标,使用下面的代码片段检索 body.getPosition().y 游戏启动时,演员的y坐标从2.5的正确位置变为3.6的错误位置。然后,参与者从其更新(不正确)的y位置正常操作 我已经调试了我的代码的be
body.getPosition().y
游戏启动时,演员的y坐标从2.5的正确位置变为3.6的错误位置。然后,参与者从其更新(不正确)的y位置正常操作
我已经调试了我的代码的bejaysus,我看不到任何可能导致这种情况的原因。以前有人见过这种行为吗?这是我不知道的某种Box2D细微差别吗
游戏演员班
public abstract class GameActor extends Actor {
protected Body body;
protected UserData userData;
protected Rectangle screenRectangle;
public GameActor() {
}
public GameActor(Body body) {
this.body = body;
this.userData = (UserData) body.getUserData();
screenRectangle = new Rectangle();
}
@Override
public void act(float delta) {
super.act(delta);
if (body.getUserData() != null) {
updateRectangle();
} else {
// This means the world destroyed the body (enemy or runner went out of bounds)
remove();
}
}
public abstract UserData getUserData();
private void updateRectangle() {
screenRectangle.x = transformToScreen(body.getPosition().x - userData.getWidth() / 2);
screenRectangle.y = transformToScreen(body.getPosition().y - userData.getHeight() / 2);
screenRectangle.width = transformToScreen(userData.getWidth());
screenRectangle.height = transformToScreen(userData.getHeight());
}
protected float transformToScreen(float n) {
return Constants.WORLD_TO_SCREEN * n;
}
}
public class Runner extends GameActor {
...
@Override
public void draw(Batch batch, float parentAlpha) {
super.draw(batch, parentAlpha);
float x = screenRectangle.x - (screenRectangle.width * 0.1f);
float y = screenRectangle.y;
float width = screenRectangle.width * 1.5f;
stateTime += Gdx.graphics.getRawDeltaTime();
batch.draw(runningAnimation.getKeyFrame(stateTime, true), x, y - 40, width, screenRectangle.height);
}
}
}
跑步者等级
public abstract class GameActor extends Actor {
protected Body body;
protected UserData userData;
protected Rectangle screenRectangle;
public GameActor() {
}
public GameActor(Body body) {
this.body = body;
this.userData = (UserData) body.getUserData();
screenRectangle = new Rectangle();
}
@Override
public void act(float delta) {
super.act(delta);
if (body.getUserData() != null) {
updateRectangle();
} else {
// This means the world destroyed the body (enemy or runner went out of bounds)
remove();
}
}
public abstract UserData getUserData();
private void updateRectangle() {
screenRectangle.x = transformToScreen(body.getPosition().x - userData.getWidth() / 2);
screenRectangle.y = transformToScreen(body.getPosition().y - userData.getHeight() / 2);
screenRectangle.width = transformToScreen(userData.getWidth());
screenRectangle.height = transformToScreen(userData.getHeight());
}
protected float transformToScreen(float n) {
return Constants.WORLD_TO_SCREEN * n;
}
}
public class Runner extends GameActor {
...
@Override
public void draw(Batch batch, float parentAlpha) {
super.draw(batch, parentAlpha);
float x = screenRectangle.x - (screenRectangle.width * 0.1f);
float y = screenRectangle.y;
float width = screenRectangle.width * 1.5f;
stateTime += Gdx.graphics.getRawDeltaTime();
batch.draw(runningAnimation.getKeyFrame(stateTime, true), x, y - 40, width, screenRectangle.height);
}
}
}
通过在整个代码中打印body.getPosition().y来尝试定位bug的确切位置(就像二分法一样)。@P–risdouad是一个很好的起点。在游戏阶段的第二次和第三次迭代中,通过act(浮动增量)方法,Y位置分两个阶段移动到错误的位置。这看起来像是一个简单的错误,但我们需要一些代码来找出它。是的,没有代码,这将很难。记住调用演员的setBounds方法。