2013-01-31 26 views
1

我有一個使用觸摸板旋轉的精靈。我遇到的唯一問題是當觸摸板沒有移動時,旋轉停止。即使觸摸板的Y值爲100%,如果仍保持精靈旋轉停止。無論觸控板是否在移動,我如何保持旋轉不變?我的代碼是低於如何通過觸摸板保持旋轉不變?

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

您在Touchpad上註冊ChangeListener。它的changed方法僅在觸摸板上發生變化時纔會調用。

您應該在render()方法中輪詢觸控板的狀態(因此每次繪製一個框架時,如果觸控板處於活動狀態,則更新旋轉),而不是根據輸入事件進行更新。

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

您可能想要縮放旋轉速率,使其與時間成正比,而不是幀速率。見Gdx.graphics.getDeltaTime()

+0

非常感謝指針,如果我打電話給stage.act(Gdx.graphics.getDeltaTime());那會有訣竅嗎? – AspiretoCode