2016-05-29 231 views
1

我有一個簡單的問題 - 我試圖阻止拖動演員,但我看到奇怪的效果 - 當我試圖拖動演員開始閃爍並隨機更改位置。這裏是一個簡短的視頻:https://youtu.be/KpGujQ7KHc0奇怪的行爲與touchDragged

演員很簡單:

public class Item extends Actor { 
    private String id; 
    private TextureRegion texture; 

    public Item() { 
     this.id = "passport"; 

     texture = PicturesData.getPicture(getId()); 
     setSize(texture.getRegionWidth(), texture.getRegionHeight()); 

     addListener(new DragListener() { 
      @Override 
      public boolean touchDown(InputEvent event, float x, float y, int pointer, int button) { 
       return true; 
      } 

      @Override 
      public void touchDragged(InputEvent event, float x, float y, int pointer) { 
       Log.add("Dragged: " + x + ", " + y); 
       setPosition(x, y); 
      } 
     }); 
    } 

    public String getId() { 
     return id; 
    } 

    @Override 
    public void draw(Batch batch, float parentAlpha) { 
     batch.draw(texture, getX(), getY()); 
    } 
} 

屏幕是基本太:

public class GameScreen implements Screen { 
    private Stage stage; 

    public GameScreen() { 
     stage = new Stage(new StretchViewport(800, 600)); 
     Gdx.input.setInputProcessor(stage); 

     Item item = new Item(); 

     stage.addActor(item); 
    } 

    @Override 
    public void show() { 
    } 

    @Override 
    public void render(float delta) { 
     Gdx.gl.glClearColor(0, 0, 0, 1); 
     Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT | GL20.GL_DEPTH_BUFFER_BIT); 

     stage.act(delta); 
     stage.draw(); 
    } 

    @Override 
    public void resize(int width, int height) { 

    } 

    @Override 
    public void pause() { 

    } 

    @Override 
    public void resume() { 

    } 

    @Override 
    public void hide() { 

    } 

    @Override 
    public void dispose() { 

    } 
} 

我不知道爲什麼它的發生,你能幫幫我嗎?

+0

還有其它地方在你的代碼中這個演員的位置發生變化? –

+0

不,我用屏幕代碼更新了我的帖子。這也是非常基本的。 – Pasha

回答

1

Pasha,您的DragListener沒有任何問題。使用這個項目(起始位置硬編碼):

public class Item extends Image { 
    private String id; 

    public Item(String id) { 
     super(PicturesData.getPicture(getId()); 
     this.id = id; 
     setPosition(50, 50); 
     addListener(new DragListener() { 
      @Override 
      public boolean touchDown(InputEvent event, float x, float y, int pointer, int button) { 
       return true; 
      } 

      @Override 
      public void touchDragged(InputEvent event, float x, float y, int pointer) { 
       moveBy(x - getWidth()/2, y - getHeight()/2); 
      } 
     }); 
    } 

    public String getId() { 
     return id; 
    } 
} 
+0

同樣的錯誤對我來說。你使用什麼樣的libgdx版本?可以嗎?我的版本是1.9。 3,但我也試過1.9.1 ...也許我應該在libgdx的github中報告它... – Pasha

+0

立即嘗試代碼:-) – WonderfulWorld

+0

是的,它的工作原理!他們應該在javadoc中寫入'touchDragged'中的'X'和'Y'不是新的位置,而是與前一個位置的delta!非常感謝你! – Pasha

0

我認爲你的DragListener不能正常工作的原因是因爲你已經覆蓋了touchDown函數,這是偵聽器正常工作所必需的。只需將您的代碼更改爲:

addListener(new DragListener() { 
     @Override 
     public void drag(InputEvent event, float x, float y, int pointer) { 
      Log.add("Dragged: " + x + ", " + y); 
      setPosition(x, y); 
     } 
    }); 
+0

據我所知,覆蓋'touchDown'是正確的。從用於'touchDragged'的javadoc:_當鼠標按鈕或手指觸摸移動到任何位置時調用,但僅當touchDown先前爲鼠標按鈕或touch_返回true時調用。無論如何,我試圖刪除'touchDown' - 結果仍然是: – Pasha

+0

嘗試。相同的錯誤... – Pasha