我正在为 Android 平台开发我的第一个视频游戏,作为一个晚上和周末的项目。
它进展顺利,但我对控制灵敏度非常不满意。
在这个游戏中,您在屏幕上左右移动一个对象。屏幕底部是一个“触摸板”,是你的手指应该休息的地方。
/-------------------------\
| |
| |
| |
| Game Area |
| |
| |
| |
| |
| |
/-------------------------\
| |
| Touch Area |
| |
\-------------------------/
我目前正在使用状态变量来保存“MOVING_LEFT,MOVING_RIGHT,NOT_MOVING”,并根据该变量更新每帧播放器对象的位置。
但是,我读取触摸屏输入并设置此状态变量的代码要么过于敏感,要么过于滞后,具体取决于我如何调整它:
public void doTouch (MotionEvent e) {
int action = e.getAction();
if (action == MotionEvent.ACTION_DOWN) {
this.mTouchX = (int)e.getX();
this.mTouchY = (int)e.getY();
}
else if (action == MotionEvent.ACTION_MOVE) {
if ((int)e.getX() >= this.mTouchX) {
this.mTouchX = (int)e.getX();
this.mTouchY = (int)e.getY();
if (this.TouchRect.contains(this.mTouchX, this.mTouchY)) {
this.mTouchDirection = MOVING_RIGHT;
}
}
else if ((int)e.getX() <= this.mTouchX) {
this.mTouchX = (int)e.getX();
this.mTouchY = (int)e.getY();
if (this.TouchRect.contains(this.mTouchX, this.mTouchY)) {
this.mTouchDirection = MOVING_LEFT;
}
}
else {
this.mTouchDirection = NOT_MOVING;
}
}
else if (action == MotionEvent.ACTION_UP) {
this.mTouchDirection = NOT_MOVING;
}
}
这个想法是,当有任何移动时,我会检查用户手指的先前位置,然后找出移动玩家的方向。
这不太好用,我想这里有一些 iPhone/Android 开发人员已经弄清楚如何通过触摸屏进行良好的控制,并可以提供一些建议。