简体   繁体   English

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

[英]Keep sprite on screen when movement keys are pressed

How can I make sure the spaceshipSprite stay on the screen when the keys are pressed. 按下按键时如何确保spaceshipSprite停留在屏幕上。 I don't want the spaceship to go off the screen. 我不想让飞船离开屏幕。

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();
}

Move the camera instead of, or in tandem with, the sprite. 代替或与精灵一起移动相机。 Use the translate method on the camera. 在相机上使用translate方法。 See https://code.google.com/p/libgdx/wiki/OrthographicCamera 参见https://code.google.com/p/libgdx/wiki/OrthographicCamera

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM