使用TouchScreens进行游戏控制

Fly*_*wat 8 iphone user-interface android touchscreen

我正在为Android平台制作我的第一个视频游戏,作为一个晚上和周末的项目.

它很顺利,但我对控制感觉非常不满意.

在此游戏中,您可以在屏幕上左右移动对象.在屏幕的底部是各种各样的"触摸板",这是你的手指应该休息的地方.

/-------------------------\
|                         |
|                         |
|                         |
|       Game Area         |
|                         |
|                         |
|                         |
|                         |
|                         |
/-------------------------\
|                         |
|       Touch Area        |
|                         |
\-------------------------/
Run Code Online (Sandbox Code Playgroud)

我目前正在使用状态变量来保存"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;
    }       
}
Run Code Online (Sandbox Code Playgroud)

我的想法是,当有任何移动时,我会检查用户手指的先前位置,然后找出移动玩家的方向.

这不是很好,我想这里有一些IPhone/Android开发人员已经想出如何通过触摸屏进行良好的控制,并可以给出一些建议.

Tho*_*mas 2

您可以尝试类似于 Windows 上的“拖动矩形”的操作。当您在某物上按住鼠标按钮时,直到鼠标移动到鼠标按下位置周围的小区域之外时,才会开始拖动操作。原因是单击时很难将光标保持在同一像素上。

因此,第一次尝试可能(int)e.getX() >= this.mTouchX + DEAD_ZONE与其他情况类似,其中DEAD_ZONE是一个小整数。

然而,这并不涉及在同一个行程中转身。您可以通过仅在右转后当前位置距离最后位置左侧至少有DEAD_ZONE像素时向左转来解决此问题,反之亦然。