使用触摸屏进行游戏控制

发布于 2024-07-15 06:28:54 字数 1930 浏览 1 评论 0原文

我正在开发我的第一个 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 开发人员已经弄清楚如何通过触摸屏进行良好的控制,并且可以提供一些建议。

I'm working on my first video game for the Android platform as a bit of a nights and weekends project.

It is coming along nicely, but I am very unhappy with the control sensativity.

In this game, you move an object left and right on the screen. On the bottom of the screen is a "touchpad" of sorts, which is where your finger should rest.

/-------------------------\
|                         |
|                         |
|                         |
|       Game Area         |
|                         |
|                         |
|                         |
|                         |
|                         |
/-------------------------\
|                         |
|       Touch Area        |
|                         |
\-------------------------/

I am currently using a state variable to hold "MOVING_LEFT, MOVING_RIGHT, NOT_MOVING" and am updating the location of the player object each frame based on that variable.

However, my code that reads the touchscreen input and sets this state variable is either too sensative, or too laggy, depending on how I tweak it:

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;
    }       
}

The idea is that when there is any movement, I check the previous location of the users finger and then figure out what direction to move the player.

This doesn't work very well, I figure there are some IPhone/Android developers on here who have figured out how to do good controls via a touchscreen and can give some advice.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

铁轨上的流浪者 2024-07-22 06:28:54

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

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

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

You could try something similar to "drag rectangles" on Windows. When you hold down the mouse button on something, you don't start a drag operation until the mouse moves outside a small region around the mouse-down location. Reason being that it's very hard to keep the cursor on the same pixel while clicking.

So a first attempt could be (int)e.getX() >= this.mTouchX + DEAD_ZONE and similar for the other case, where DEAD_ZONE is a small integer.

This does not deal with turning around in the same stroke, however. You could deal with that by only turning left when the current position is at least DEAD_ZONE pixels to the left of the last position after turning right, and vice versa.

饭团 2024-07-22 06:28:54

一个明显的问题是,你没有考虑到自上次触摸以来的时间。

我建议您将玩家的触摸视为在 -x/+x-y/+y 模拟范围内所需运动的表达,然后执行实际的运动其他地方根据时间。

例如,

objectPos += objectPos + (joyPos * timeDelta * maxSpeed);

如果对象的最大速度为 10ms-1 并且玩家的手指在控制板上的位置为 0.5,则对象每秒将移动 5 米。 如果您的游戏以 10 fps 运行,则每帧对象将移动 0.5 米。

这些数字是虚构的,但希望证明需要将控制与运动分开,然后考虑时间因素。

One obvious problem is that nowhere do you take account of the time since the last touch.

I would suggest you treat the players touch as an expression of desired movement on an analogue range from -x/+x and -y/+y, then perform the actual movement elsewhere based on time.

E.g.

objectPos += objectPos + (joyPos * timeDelta * maxSpeed);

So if the max-speed of your object is 10ms-1 and the players finger is at 0.5 on the control pad then the object would be moving 5 meters every second. If your game is running at 10fps then each frame the object would move 0.5 meters.

These figures are fictional, but hopefully demonstrate the need to separate control from movement and then factor in time.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文