所以,我试图实现触发游戏中主角移动的键。 这是我的代码:
public class Play implements Screen{
变量:
World world;
Box2DDebugRenderer b2Dr;
OrthographicCamera camera;
OrthographicCamera hudCamera;
private SpriteBatch batch;
private Body toniBody;
private RHContactListener rhcl;
渲染内容(请注意,我有一个名为InputController的java类用于setInputProcessor,它扩展了InputAdapter并且它为Down和Up状态设置了某些键):
@Override
public void render(float delta) {
Gdx.gl.glClearColor(1, 1, 1, 1);
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
Gdx.input.setInputProcessor(new InputController());
RHInput.update();
camera.update();
handleInput();
world.step(delta, 6, 2);
batch.setProjectionMatrix(camera.combined);
batch.begin();
batch.end();
b2Dr.render(world, camera.combined);
}
这是相机设置为某个缩放的位置
@Override
public void resize(int width, int height) {
camera.viewportWidth = width/24;
camera.viewportHeight = height/24;
camera.update();
}
屏幕上的内容:
@Override
public void show() {
world = new World(new Vector2(0, -9.81f),true);
rhcl = new RHContactListener();
world.setContactListener(rhcl);
b2Dr = new Box2DDebugRenderer();
batch = new SpriteBatch();
camera= new OrthographicCamera();
这里我定义了我的场景,地面和玩家的物体:
////Box2D////
//CREATE GROUND
//body definition
BodyDef bDef = new BodyDef();
bDef.position.set(0,-14);//meters
bDef.type = BodyDef.BodyType.StaticBody;
Body body = world.createBody(bDef);
//shape definition
PolygonShape ground = new PolygonShape();
ground.setAsBox(25, 1);//meters
//fixture definition
FixtureDef fDef = new FixtureDef();
fDef.friction= .5f;
fDef.shape = ground;
fDef.filter.categoryBits = B2DVariables.BIT_ground;//colision type
fDef.filter.maskBits = B2DVariables.BIT_can;//matching collision type
body.createFixture(fDef).setUserData("ground");
//CREATE TONI
//body definition
bDef.position.set(-10,-10);//meters
bDef.type = BodyDef.BodyType.DynamicBody;
toniBody = world.createBody(bDef);
//shape definition
PolygonShape tBox = new PolygonShape();
tBox.setAsBox(0.5f,2);//meters
//fixture definition
fDef.shape = tBox;
fDef.filter.categoryBits = B2DVariables.BIT_can;//colision type can step on ground
fDef.filter.maskBits = B2DVariables.BIT_ground;
toniBody.createFixture(fDef).setUserData("toni");
//Toni's foot the Ground Sensor
tBox.setAsBox(.2f , 0.2f, new Vector2(0, -2),0);
fDef.shape = tBox;
fDef.filter.categoryBits = B2DVariables.BIT_can;
fDef.filter.maskBits = B2DVariables.BIT_ground;
fDef.isSensor = true;
toniBody.createFixture(fDef).setUserData("toniFoot");
ground.dispose();
tBox.dispose();
}
这是某个键触发动作的部分
public void handleInput(){
//press W key aka Up key or BTNup
if (RHInput.isDown(RHInput.BTNup)){
if(rhcl.isPlayerOnGround()) {
toniBody.setLinearVelocity(0,100);
System.out.println("Toni jumps");
}
}
if (RHInput.isDown(RHInput.BTNright)){
rhcl.isPlayerOnGround();
toniBody.setLinearVelocity(5,0);
System.out.println("Toni walks right");
}else {
toniBody.setLinearVelocity(0,0);
}
if (RHInput.isDown(RHInput.BTNleft)){
if(rhcl.isPlayerOnGround()) {
toniBody.setLinearVelocity(-5,0);
System.out.println("Toni walks left");
}
else {
toniBody.setLinearVelocity(0,0);
}
}
if (RHInput.isPressed(RHInput.BTNdown)){
System.out.println("pressed s");
}
if (RHInput.isDown(RHInput.BTNdown)){
System.out.println("holding s");
}
}
其他libGDX东西
@Override
public void hide() {
dispose();
}
@Override
public void pause() {
}
@Override
public void resume() {
}
@Override
public void dispose() {
world.dispose();
b2Dr.dispose();
}
}
代码正在运行。角色以线速度左右移动。但它不会跳得很高,当它下降时它会非常慢。怎么解决这个问题?
谢谢!
答案 0 :(得分:1)
嗯......我从未使用过libgdx,但一直在阅读如何在这里实现跳转http://obviam.net/index.php/getting-started-in-android-game-development-with-libgdx-tutorial-part-3-jumping-gravity-and-movement/并检查代码中是否添加了物理。我发现你有太多的摩擦(大约.5左右),所以精灵应该看起来像是浮动的。并且在世界构造函数中以5米/秒的速度跳跃,而重力代表9.8米/秒^ 2,因此它将在一秒钟内减速而不计数,再次,你已添加的摩擦阻力......所以,删除摩擦,想象,就像在教程中一样。