java - 不会继续执行 ACTION_DOWN 上的语句

标签 java android events ontouchlistener

我正在为我的任务制作一款月球着陆器游戏

我有一个按钮并为其设置了一个 onTouchListener (不是 onClick) 当按钮按下或 ACTION_DOWN 时,火箭会向上飞行并在 ACTION_UP 时停止加速。

目前它只在 ACTION_DOWN 上执行一次,因此它的行为就像一个 onClick 按钮。

如有任何帮助,我们将不胜感激

  • gView 是游戏类的一个实例,所有魔法都在这里发生
  • gView.mainThruster() 调用游戏类中使飞船向上飞行的方法,例如 Y -= 10;

    mThrust = (Button) findViewById(R.id.mainThrustBtn);    
    mThrust.setOnTouchListener(new OnTouchListener(){
    @Override
    public boolean onTouch(View v, MotionEvent event) {
    
        if(event.getAction() == MotionEvent.ACTION_DOWN) {
            if(fuel > 0){
    
                  gView.checkFuel(fuel);
              gView.mainThruster();
              mThrustSound.start();
              fuel -= 0.5;
              fuelGauge.setProgress((int)fuel);
              }
         }
         else if(event.getAction() == MotionEvent.ACTION_UP){
    
            mThrustSound.pause();
            //mThrustSound.release();   
         }
         else{
            //do nothing
         }
            return true;
         }
     });
    

最佳答案

文档清楚地说明了这一点:

On pointing devices with source class SOURCE_CLASS_POINTER such as touch screens, the pointer coordinates specify absolute positions such as view X/Y coordinates. Each complete gesture is represented by a sequence of motion events with actions that describe pointer state transitions and movements. A gesture starts with a motion event with ACTION_DOWN that provides the location of the first pointer down. As each additional pointer that goes down or up, the framework will generate a motion event with ACTION_POINTER_DOWN or ACTION_POINTER_UP accordingly. Pointer movements are described by motion events with ACTION_MOVE. Finally, a gesture end either when the final pointer goes up as represented by a motion event with ACTION_UP or when gesture is canceled with ACTION_CANCEL.

因此,您应该期望获得一个ACTION_DOWN事件,然后是多个ACTION_MOVE事件。听起来基本上,如果你想处理 Action ,你也需要响应 ACTION_MOVE ,并且如果你想在触摸发生时连续做某事,你可能想要某种计时器事件(因为您不会生成新的运动事件 - 仅触摸和按住不涉及运动)。

关于java - 不会继续执行 ACTION_DOWN 上的语句,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19354346/

相关文章:

Python 处理系统关闭

java - 无法通过 java api 将 google 格式的图像文件上传到 Google Doc

java.lang.UnsupportedClassVersionError - jdk版本

android - Firebase 数据库带宽计算

java - 如何从java中的另一个页面调用函数?

java - 我如何避免这种忙等待?

Java 接口(interface) - 返回方法调用

Java:如何将列表转换为泛型类型的数组

Jquery,为每个ajax调用独立注册一个函数

android - 如何检测 Android 上的触摸输入