2017-08-02 100 views
1

我在Android Studio的libGDX中遇到了代碼問題。 我試圖讓對象在按下屏幕時執行操作。如果我放棄了,以前的行動應該中止,應該開始第二步。當我在屏幕上再次按下時,第二個動作應該再次停止,並在我按住時開始第一個動作。LibGDX:按下觸摸屏時的操作以及釋放觸摸屏時的操作

我很遺憾不知道我該怎麼做,而且互聯網不幸也沒有關於它的確切信息。 這也意味着我沒有代碼,我可以將它顯示爲一個輔助位置。

如果有人有解決方案,或者可以幫助我,我會很高興。 :)

回答

0

您可以用這種方式:

view.setOnTouchListener(new View.OnTouchListener() {   
    @Override 
    public boolean onTouch(View v, MotionEvent event) { 
     switch(event.getAction()) { 
      case MotionEvent.ACTION_DOWN: 
       // PRESSED 
       return true; // if you want to handle the touch event 
      case MotionEvent.ACTION_UP: 
       // RELEASED 
       return true; // if you want to handle the touch event 
     } 
     return false; 
    } 
}); 

讓我知道,如果這有助於..

+0

感謝您的幫助,但Abhishek Aryan的解決方案也有效! – Eron

1

我會用其中有被稱爲觸摸一個布爾值的輸入監聽。然後在touchDown事件中將觸摸設置爲true。再次在touchUp事件的輸入偵聽器中將觸摸設置爲false。

public class MyController implements InputProcessor { 
    @Override 
    public boolean touchUp(int screenX, int screenY, int pointer, int button) { 
     touching = false; 
    } 

    @Override 
    public boolean touchDown(int screenX, int screenY, int pointer, int button) { 
     touching = true; 
    } 
    //.. more methods etc 

} 

在應用程序中創建這個myController的,並設置爲與inputListsner:

controller = new MyController(); 
Gdx.input.setInputProcessor(controller); 

現在,當用戶觸摸則可以檢測和你需要什麼都行動:

if(controller.touching){ 
    //user is touching do touching action 
}else{ 
    // user isn't touching do the non touchin action 
} 
+0

感謝您的幫助,但Abhishek Aryan的解決方案也可行! – Eron

+0

這是更好的方法 –

0

你可以這樣使用裏面的渲染方法:

if(Gdx.input.isTouched()){ 

    //whether the screen is currently touched. 
}else{ 

} 

  1. 您可以通過後實施InputProcessor類中Gdx.input.setInputProcessor(..)

  2. 然後覆蓋該接口的touchDown(..)touchUp(...)方法,

  3. 選擇標誌或其他技術爲您的要求。
+1

我試過這個,很有用,非常感謝! – Eron