I am making a lunar lander game for my assignment
i have a button and set an onTouchListener to it (not onClick) when the button is down or ACTION_DOWN the rocket flies up and stops acceleration on ACTION_UP.
currently it is only performing this once on ACTION_DOWN so it is behaving like an onClick button.
any help would be appreciated
gView.mainThruster() calls the method in the game class that causes the spaceship to fly upwards e.g 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;
}
});
The documentation makes this reasonably clear:
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.
So you should expect to get a single ACTION_DOWN
event, followed by multiple ACTION_MOVE
events. It sounds like basically you need to respond to ACTION_MOVE
as well if you want to handle movements, and if you want to do something continuously while the touch takes place, you probably want some kind of timer event (as you won't be generating new motion events - there's no motion involved in just touching and holding).
See more on this question at Stackoverflow