我正在为我的任务制作一款月球着陆器游戏
我有一个按钮并为其设置了一个 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/