Game Development Reference
In-Depth Information
Depending on the touch event action, ACTION_DOWN or ACTION_UP , you simply send a key
event to the native layer with the following code:
public static void sendNativeKeyEvent (int type, int sym) {
try {
Natives.keyEvent(type, sym);
} catch (UnsatisfiedLinkError e) {
Log.e(TAG, e.toString());
}
}
Listing 5-11 shows the setupPanControls() function for the Up, Down, Left, and Right
buttons of the Doom controller.
Listing 5-11. Controller Event Setup
private void setupPanControls() {
// Up
findViewById(R.id.btn_up).setOnTouchListener(
new View.OnTouchListener(){
public boolean onTouch(View v, MotionEvent evt) {
int action = evt.getAction();
if ( action == MotionEvent.ACTION_DOWN) {
Natives.sendNativeKeyEvent(Natives.EV_KEYDOWN
, DoomTools.KEY_UPARROW);
}
else if ( action == MotionEvent.ACTION_UP) {
Natives.sendNativeKeyEvent(Natives.EV_KEYUP
, DoomTools.KEY_UPARROW);
}
return true;
}
});
// Down
findViewById(R.id.btn_down).setOnTouchListener(
new View.OnTouchListener(){
public boolean onTouch(View v, MotionEvent evt) {
int action = evt.getAction();
if ( action == MotionEvent.ACTION_DOWN) {
Natives.sendNativeKeyEvent(Natives.EV_KEYDOWN
, DoomTools.KEY_DOWNARROW);
}
else if ( action == MotionEvent.ACTION_UP) {
Natives.sendNativeKeyEvent(Natives.EV_KEYUP
, DoomTools.KEY_DOWNARROW);
}
return true;
}
});
 
Search WWH ::




Custom Search