Game Development Reference
In-Depth Information
In the constructor, we register the handler as an OnTouchListener and set up the Pool that we
use to recycle TouchEvent s. We also store the scaleX and scaleY parameters that are passed to
the constructor (ignore them for now).
public boolean onTouch(View v, MotionEvent event) {
synchronized ( this ) {
TouchEvent touchEvent=touchEventPool.newObject();
switch (event.getAction()) {
case MotionEvent. ACTION_DOWN :
touchEvent.type=TouchEvent. TOUCH_DOWN ;
isTouched= true ;
break ;
case MotionEvent. ACTION_MOVE :
touchEvent.type=TouchEvent. TOUCH_DRAGGED ;
isTouched= true ;
break ;
case MotionEvent. ACTION_CANCEL :
case MotionEvent. ACTION_UP :
touchEvent.type=TouchEvent. TOUCH_UP ;
isTouched= false ;
break ;
}
touchEvent.x=touchX=( int )(event.getX() * scaleX);
touchEvent.y=touchY=( int )(event.getY() * scaleY);
touchEventsBuffer.add(touchEvent);
return true ;
}
}
The onTouch() method achieves the same outcome as our KeyboardHandler 's onKey()
method; the only difference is that now we handle TouchEvent is instead of KeyEvent is All the
synchronization, pooling, and MotionEvent handling are already known to us. The only interesting
thing is that we multiply the reported x and y coordinates of a touch event by scaleX and scaleY .
This is important to remember because we'll return to it in the following sections.
public boolean isTouchDown( int pointer) {
synchronized ( this ) {
if if(pointer == 0)
return isTouched;
else
return false ;
}
}
public int getTouchX( int pointer) {
synchronized ( this ) {
return touchX;
}
}
Search WWH ::




Custom Search