【发布时间】:2015-10-27 15:15:32
【问题描述】:
我正在使用 libgdx 和 box2d,我想让我的玩家的身体跳跃。当我尝试使用某些方法时,即使只是为了移动,也没有任何反应。
//gravity Vector2
Vector2 Gravity = new Vector2(0,-9.8);
//Box2d World
World world = New World(gravity);
//then I created my player body
public void createPlayer(){
BodyDef def = new BodyDef();
def.type = BodyDef.BodyType.DynamicBody;
def.fixedRotation = true;
def.position.set(80, 200);
player = world.createBody(def);
PolygonShape shape = new PolygonShape();
shape.setAsBox(20, 20);
player.createFixture(shape, 1f);
shape.dispose();
}
//this is the jump method
public void jump(){
if(Gdx.input.justTouched() == true) {
player.applyForceToCenter(0, 900, false);
System.out.println("touched");//to make sure the touch is working`
}
}
什么都没有发生,玩家的身体只是下落,直到它撞到一个静态的身体,每次我触摸屏幕时它只会打印出“触摸”的字符串。
更新
不确定这是否是它不起作用的原因,我有两个类,一个用于渲染,一个用于更新。在更新类上,我设置了 world.step() 而不是直接设置到我的身体所在的类。
//class for updating
public void update(float deltaTime){
physics.world.step(1 / 60f, 6, 2);
physics.jump(); //calling jump method from Physics class
}
渲染类也是一样,Box2dDebugRenderer 在我的身体所在的地方是分开的
更新
我解决了这个问题,连接到 Update 和 Render 类的物理类引用了主类的 create 方法,我不明白,因为这是我为其他类的构造函数所做的。
public void create(){
physics = new Physics(); //object for the Physics constructor.
}
【问题讨论】: