【问题标题】:LibGDX: Action when touchscreen is pressed and action when touchscreen is releasedLibGDX:按下触摸屏时的动作和释放触摸屏时的动作
【发布时间】:2017-08-02 09:37:56
【问题描述】:

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

不幸的是,我不知道该怎么做,不幸的是,互联网上也没有关于它的确切信息。 这也意味着我没有代码,我可以将其显示为辅助位置。

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

【问题讨论】:

    标签: java libgdx touch pressed


    【解决方案1】:

    我会使用一个输入侦听器,它有一个称为触摸的布尔值。然后在touchDown 事件中将touch 设置为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
    }
    

    【讨论】:

    • 感谢您的帮助,但 Abhishek Aryan 的解决方案也有效!
    • 这是更好的方法
    【解决方案2】:

    你可以这样使用:

    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;
        }
    });
    

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

    【讨论】:

    • 感谢您的帮助,但 Abhishek Aryan 的解决方案也有效!
    【解决方案3】:

    你可以在 render 方法中这样使用:

    if(Gdx.input.isTouched()){
    
        //whether the screen is currently touched.
    }else{
    
    }
    

    1. 你可以在Gdx.input.setInputProcessor(..)内部传递InputProcessor实现的类

    2. 然后覆盖该接口的touchDown(..)touchUp(...)方法,

    3. 根据您的要求选择标志或其他技术。

    【讨论】:

      【解决方案4】:

      点击:

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

      未开发:

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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2019-03-12
        • 2016-08-13
        • 1970-01-01
        • 2011-05-09
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多