Android游戏开发教程----游戏控制
游戏控制是我们的游戏技术基础的最后一部分,在Android 游戏中我们可以通过按键,触摸屏还有传感器来控制游戏。
按键控制
按键并非收有安卓手机都拥有,实际上Android平板和新版本的Android操作系统中,我们能发现实体按键正有逐渐消失的趋势。
按键事件包括onKeyUp和onKeyDown等事件,下面通过一个按键事件的使用案例来了解按键控制技术。
[java] public class KeyInputActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(new TextView(this));
}
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
switch(keyCode){
case KeyEvent.KEYCODE_DPAD_CENTER:
Log.v("onKeyDown", "按下:中键");
break;
case KeyEvent.KEYCODE_DPAD_UP:
Log.v("onKeyDown", "按下:上方向键");
break;
case KeyEvent.KEYCODE_DPAD_DOWN:
Log.v("onKeyDown", "按下:下方向键");
break;
case KeyEvent.KEYCODE_DPAD_LEFT:
Log.v("onKeyDown", "按下:左方向键");
break;
case KeyEvent.KEYCODE_DPAD_RIGHT:
Log.v("onKeyDown", "按下:右方向键");
break;
}
return super.onKeyDown(keyCode, event);
}
@Override
public boolean onKeyUp(int keyCode, KeyEvent event) {
switch(keyCode){
case KeyEvent.KEYCODE_DPAD_CENTER:
Log.v("onKeyDown", "释放:中键");
break;
case KeyEvent.KEYCODE_DPAD_UP:
Log.v("onKeyDown", "释放:上方向键");
break;
case KeyEvent.KEYCODE_DPAD_DOWN:
Log.v("onKeyDown", "释放:下方向键");
break;
case KeyEvent.KEYCODE_DPAD_LEFT:
Log.v("onKeyDown", "释放:左方向键");
break;
case KeyEvent.KEYCODE_DPAD_RIGHT:
Log.v("onKeyDown", "释放:右方向键");
break;
}
return super.onKeyUp(keyCode, event);
}
}
public class KeyInputActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(new TextView(this));
}
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
switch(keyCode){
case KeyEvent.KEYCODE_DPAD_CENTER:
Log.v("onKeyDown", "按下:中键");
break;
case KeyEvent.KEYCODE_DPAD_UP:
Log.v("onKeyDown", "按下:上方向键");
break;
case KeyEvent.KEYCODE_DPAD_DOWN:
Log.v("onKeyDown", "按下:下方向键");
break;
case KeyEvent.KEYCODE_DPAD_LEFT:
Log.v("onKeyDown", "按下:左方向键");
break;
case KeyEvent.KEYCODE_DPAD_RIGHT:
Log.v("onKeyDown", "按下:右方向键");
break;
}
return super.onKeyDown(keyCode, event);
}
@Override
public boolean onKeyUp(int keyCode, KeyEvent event) {
switch(keyCode){
case KeyEvent.KEYCODE_DPAD_CENTER:
Log.v("onKeyDown", "释放:中键");
break;
case KeyEvent.KEYCODE_DPAD_UP:
Log.v("onKeyDown", "释放:上方向键");
break;
case KeyEvent.KEYCODE_DPAD_DOWN:
Log.v("onKeyDown", "释放:下方向键");
break;
case KeyEvent.KEYCODE_DPAD_LEFT:
Log.v("onKeyDown", "释放:左方向键");
break;
case KeyEvent.KEYCODE_DPAD_RIGHT:
Log.v("onKeyDown", "释放:右方向键");
break;
}
return super.onKeyUp(keyCode, event);
}
}
说明:
keyCode为键值,手机中每一个按钮都拥有一个完全独立的键值,通过按键键值就可以确定当前按下的是哪一个按键。
KeyEvent为按键事件,该对象中保存着当前按键的所有信息。比如:按键发生的时间,按键发生的次数,按键发生的类型等等。
触摸控制
在Android平台中,触摸控制是基础,这也是移动平台优势所在,因为触摸能提供直观的更加人性化的操作。触摸就分为单点触摸控制与多点触摸控制,下面通
补充:移动开发 , Android ,