Game Development Reference
In-Depth Information
}
return true;
}
});
// Right
mView.findViewById(R.id.btn_right).setOnTouchListener(
new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent evt) {
int action = evt.getAction();
final ImageButton b = (ImageButton) v;
if (action == MotionEvent.ACTION_DOWN) {
b.setImageResource(R.drawable.snes_r1);
sendEvent(MotionEvent.ACTION_DOWN,
KeyEvent.KEYCODE_DPAD_RIGHT);
} else if (action == MotionEvent.ACTION_UP) {
b.setImageResource(R.drawable.snes_r0);
sendEvent(MotionEvent.ACTION_UP,
KeyEvent.KEYCODE_DPAD_RIGHT);
}
return true;
}
});
// Events for the SELECT, START, X, Y, A, B buttons
// have been removed for simplicity. See the class SNESController.java
// for details
/**
* Send an event to the {@link ControllerListener}
* @param state Up (MotionEvent.ACTION_UP) or
* down (MotionEvent.ACTION_DOWN)
* @param btnAndroid{@link KeyEvent}
*/
private void sendEvent(int state, int btn) {
if (mListener != null) {
if (state == MotionEvent.ACTION_UP)
mListener.ControllerUp(btn);
else
mListener.ControllerDown(btn);
}
}
}
Listing 6-10 defines touch events for the buttons: UP , DOWN , LEFT , RIFGHT , START , SELECT , X , Y , A , and B .
Let's take a closer look at how the controller reacts to user events. The next fragment shows how the
select button in the controller is initialized:
// Controller select button
mView.findViewById(R.id.btn_select).setOnTouchListener(
Search WWH ::




Custom Search