2015-10-24 242 views
3

目前,我正在使用scene2d觸控板在屏幕上移動精靈。我想要做的是將所有屏幕作爲觸摸板來移動精靈,但不知道從哪裏開始。Libgdx在屏幕上的任意位置觸摸並拖動以移動精靈

  • 如果剛剛觸摸屏幕,精靈不應該移動。
  • 根據用戶將手指從初始觸摸點移開的距離,精靈將以不同的速度移動。
  • 一旦用戶將他們的手指拖動到特定半徑以外,精靈將繼續以恆定速度移動 。

基本上不用其實際使用scene2d觸摸板

+0

您將不得不實現自己的自定義'InputProcessor'來執行此操作。也許'GestureDetector'可以在這裏提供幫助,但總體來說,libgdx中沒有這樣做。我懷疑有人會爲你編碼。 – noone

+0

是不是暗示有人爲我編碼這個我只是在尋找關於從哪裏開始的建議或從誰可能已經嘗試過 –

回答

4

觸摸板基本上你得到的意見答案。

  1. 使用InputProcessor
  2. 觸摸保存的觸摸位置和觸摸拖動當前觸摸位置之間
  3. 入住距離
  4. 保存觸摸位置

小碼爲例:

class MyInputProcessor extends InputAdapter 
{ 
    private Vector2 touchPos = new Vector2(); 
    private Vector2 dragPos  = new Vector2(); 
    private float radius  = 200f; 

    @Override 
    public boolean touchDown(
      int screenX, 
      int screenY, 
      int pointer, 
      int button) 
    { 
     touchPos.set(screenX, Gdx.graphics.getHeight() - screenY); 

     return true; 
    } 

    @Override 
    public boolean touchDragged(int screenX, int screenY, int pointer) 
    { 
     dragPos.set(screenX, Gdx.graphics.getHeight() - screenY); 
     float distance = touchPos.dst(dragPos); 

     if (distance <= radius) 
     { 
      // gives you a 'natural' angle 
      float angle = 
        MathUtils.atan2(
          touchPos.x - dragPos.x, dragPos.y - touchPos.y) 
          * MathUtils.radiansToDegrees + 90; 
      if (angle < 0) 
       angle += 360; 
      // move according to distance and angle 
     } else 
     { 
      // keep moving at constant speed 
     } 
     return true; 
    } 
} 

最後你可以隨時查看libgdx類的源代碼,看看它是如何完成的。

相關問題