如何使用触摸板保持旋转恒定?

时间:2013-01-31 16:18:53

标签: android libgdx

我有一个精灵,我正在用触摸板旋转。我遇到的唯一问题是当触摸板没有移动时,旋转停止。即使触摸板处于100%Y值,如果它仍然保持精灵旋转停止。无论触控板是否移动,如何保持旋转恒定?我的代码在

之下
    public class RotationTest implements ApplicationListener {
   private OrthographicCamera camera;
   private SpriteBatch batch;
   private Texture texture;
   private Sprite sprite;
   Stage stage;
   public boolean leonAiming = true;

   @Override
   public void create() {      
      float w = Gdx.graphics.getWidth();
      float h = Gdx.graphics.getHeight();

      camera = new OrthographicCamera(1, h/w);
      batch = new SpriteBatch();

      texture = new Texture(Gdx.files.internal("data/libgdx.png"));
      texture.setFilter(TextureFilter.Linear, TextureFilter.Linear);

      TextureRegion region = new TextureRegion(texture, 0, 0, 512, 275);

      sprite = new Sprite(region);
      sprite.setSize(0.9f, 0.9f * sprite.getHeight() / sprite.getWidth());
      sprite.setOrigin(sprite.getWidth()/2, sprite.getHeight()/2);
      sprite.setPosition(-sprite.getWidth()/2, -sprite.getHeight()/2);

      stage = new Stage();
      Gdx.input.setInputProcessor(stage);

      Skin skin = new Skin(Gdx.files.internal("data/uiskin.json"));
         Texture touchpadTexture = new Texture(Gdx.files.internal("data/touchpad.png"));
         touchpadTexture.setFilter(TextureFilter.Linear, TextureFilter.Linear);     
         TextureRegion background = new TextureRegion(touchpadTexture, 0, 0, 75, 75);
         TextureRegion knob = new TextureRegion(touchpadTexture, 80, 0, 120, 120);
         TextureRegionDrawable backgroundDrawable = new TextureRegionDrawable(background);
         TextureRegionDrawable knobDrawable = new TextureRegionDrawable(knob);
         final Touchpad touchpad = new Touchpad(10, new Touchpad.TouchpadStyle(backgroundDrawable, knobDrawable));
         ChangeListener listener = null;
         touchpad.addListener(new ChangeListener() {

         @Override
         public void changed(ChangeEvent event, Actor actor) {
            sprite.rotate(touchpad.getKnobPercentY());
         }
      });

            touchpad.setBounds(15, 15, 225, 225);
         stage.addActor(touchpad);

   }

   @Override
   public void dispose() {
      batch.dispose();
      texture.dispose();
   }

   @Override
   public void render() {      
      Gdx.gl.glClearColor(1, 1, 1, 1);
      Gdx.gl.glClear(GL10.GL_COLOR_BUFFER_BIT);

      batch.setProjectionMatrix(camera.combined);
      batch.begin();
      sprite.draw(batch);
      batch.end();
      stage.act();
      stage.draw();
   }

感谢您的帮助!

1 个答案:

答案 0 :(得分:1)

您正在ChangeListener上注册Touchpad。只有在触摸板上发生变化时才会调用changed方法。

您应该在render()方法中轮询触摸板的状态,而不是响应输入事件进行更新(因此,每次绘制一帧时,如果触摸板处于活动状态,则更新旋转)。

if (touchpad.isTouched()) {
    sprite.rotate(touchpad.getKnobPercentY());
}

您可能希望缩放旋转速率,使其与时间成比例而不是帧速率。请参阅Gdx.graphics.getDeltaTime()