按下移动键时,将精灵保持在屏幕上

时间:2013-05-23 03:53:20

标签: java libgdx sprite

如何在按下按键时确保spaceshipSprite保持在屏幕上。我不希望宇宙飞船离开屏幕。

public void render() {      
    if(Gdx.input.isKeyPressed(Keys.DPAD_LEFT)) 
      movex -= Gdx.graphics.getDeltaTime() *speed;
    if(Gdx.input.isKeyPressed(Keys.DPAD_RIGHT)) 
      movex += Gdx.graphics.getDeltaTime() * speed;
    if(Gdx.input.isKeyPressed(Keys.DPAD_UP)) 
      moveY += Gdx.graphics.getDeltaTime() * speed;  
    if(Gdx.input.isKeyPressed(Keys.DPAD_DOWN)) 
      moveY -= Gdx.graphics.getDeltaTime() * speed;
    Gdx.gl.glClearColor(0,0,0,0);
    Gdx.gl.glClear(GL10.GL_COLOR_BUFFER_BIT);
    Gdx.gl.glViewport(0, 0, Gdx.graphics.getWidth(), Gdx.graphics.getHeight());

    batch.setProjectionMatrix(camera.combined);
    batch.begin();
    //sprite.draw(batch);
    batch.draw(background, 0, 0);
    batch.draw(spaceshipSprite, movex, moveY );
    //spaceshipSprite.draw(batch);
    batch.end();
}

1 个答案:

答案 0 :(得分:0)

移动相机而不是或与精灵一起移动相机。在相机上使用translate方法。见https://code.google.com/p/libgdx/wiki/OrthographicCamera

相关问题