2017-05-07 36 views
1

因此,在我的遊戲中,我希望擁有它,因此屏幕上某人按住的時間越長,我的角色跳躍越高。但是我不知道如何檢查是否有人按住屏幕。如何在libgdx上記錄更長的觸摸以獲得更高的跳轉

我現在的嘗試是要做到這一點: 而在更新方法

public void handleInput(float dt) { 
    if (Gdx.input.isTouched()) { 
     if (sheep.getPosition().y != sheep.maxHeight && sheep.getPosition().y == sheep.minHeight) { 
       sheep.jump(1); 
     } 

     if (sheep.getPosition().y == sheep.maxHeight && sheep.getPosition().y != sheep.minHeight) { 
       sheep.jump(-1); 
     } 
    } 
} 

回答

1

我建議雙向檢測長觸摸,根據您的需要選擇一個運行它的每一幀。

  1. 您可以使用GestureListener接口longPress的方法來檢測有一個長按或不。默認情況下,長按持續時間爲1.1秒,這意味着用戶必須觸摸等於此持續時間的屏幕才能觸發longPress事件。

    @Override 
    public boolean longPress(float x, float y) { 
    
        Gdx.app.log("MyGestureListener","LONG PRESSED"); 
        return false; 
    } 
    

    將您的實現設置爲InputProcessor。

    Gdx.input.setInputProcessor(new GestureDetector(new MyGestureListener())); 
    

  • 長按僅被保持在屏幕X時間之後被調用一次。所以最好創建自己的邏輯並檢查用戶觸摸屏幕的時間。

    if (Gdx.input.isTouched()) { 
        //Finger touching the screen 
        counter++; 
    } 
    

    而且對InputListener接口touchUp根據計數器的值,使跳和復位計數器的值爲零。

    @Override 
    public boolean touchUp(int screenX, int screenY, int pointer, int button) { 
        //make jump according to value of counter 
        counter=0; //reset counter value 
        return false; 
    } 
    

    將您的實現設置爲InputProcessor。

    Gdx.input.setInputProcessor(new MyInputListener()); 
    
  • 相關問題