LibGDX:按下触摸屏时动作,触摸屏释放时动作

时间:2017-08-02 09:37:57

标签: java libgdx touch pressed

我的Android Studio中的libGDX代码有问题。 我在按屏幕时尝试让对象执行操作。如果我放手,应该中止先前的操作并开始第二个操作。当我再次按下屏幕时,第二个动作应该再次停止并在我按住时开始第一个动作。

遗憾的是,我不知道该如何做到这一点,很遗憾,互联网也没有关于它的确切信息。 这也意味着我没有代码,我可以将其显示为辅助职位。

如果有人有解决方案,或者可以帮助我,我会很高兴。 :)

4 个答案:

答案 0 :(得分:1)

我会使用一个输入监听器,它有一个名为touching的布尔值。然后在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
}

答案 1 :(得分: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;
    }
});

如果这有帮助,请告诉我。

答案 2 :(得分:0)

您可以在渲染方法中使用这种方式:

if(Gdx.input.isTouched()){

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

}

  1. 您可以在Gdx.input.setInputProcessor(..)

  2. 中传递InputProcessor已实施的课程
  3. 然后覆盖该界面的touchDown(..)touchUp(...)方法,

  4. 根据您的要求选择旗帜或其他技术。

答案 3 :(得分:0)

点击:

if (Gdx.input.justTouched()) {
//do something; }

未点击:

if (Gdx.input.getPressure()==0){
//do something-else; }