所以,我试图实现触发游戏中主角移动的键。这是我的代码:
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状态设置了某些键):
@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();
}}
密码有效了。字符以线性速度向左和向右移动。但是它不会跳得很高,当它掉下来的时候,它会很慢地掉下来。怎么才能解决这个问题?
谢谢!
发布于 2014-09-23 13:46:21
好吧..。我从未使用过libgdx,但我一直在阅读如何在这里实现跳转( http://obviam.net/index.php/getting-started-in-android-game-development-with-libgdx-tutorial-part-3-jumping-gravity-and-movement/ ),并检查您添加的物理代码。我看到你有太多的摩擦(如.5左右),所以雪碧应该是漂浮的。当重力在世界构造函数中为9.8m/秒^2时,它以每秒5米的速度跳跃,所以它会在一秒内减速所以,消除摩擦,想象一下,就像在教程中一样。
https://stackoverflow.com/questions/25995231
复制相似问题