2014-09-23 88 views
0

所以,即時嘗試實現觸發主角在遊戲中移動的按鍵。 這裏是我的代碼:玩家運動(跳轉問題)

public class Play implements Screen{ 

變量:

World world; 
Box2DDebugRenderer b2Dr; 
OrthographicCamera camera; 
OrthographicCamera hudCamera; 
private SpriteBatch batch; 
private Body toniBody; 
private RHContactListener rhcl; 

渲染的東西(注意,我有一個名爲InputController爲setInputProcessor一個Java類,它擴展InputAdapter和它全球有機紡織品標準的設定一定的鍵向下和向上的狀態):

@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

讓事情變得更快的簡單方法是增加重力。讓玩家跳得更高的簡單方法是增加你讓他跳躍的衝動/速度。 – iforce2d 2014-09-24 00:45:07

回答

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,所以它會在一秒鐘內減慢,而不會再次減少你添加的摩擦力。所以,移除摩擦,想象,就像在教程中。