简体   繁体   English

定时关键游戏的Android触摸输入延迟问题。 在调用onTouchEvent之前是否有延迟?

[英]Android touch input delay problems for a timing critical game. Is there a delay before onTouchEvent is called?

So we're working on the android version of the iPhone game Snake Race (it's a cool game). 因此,我们正在开发iPhone游戏“贪吃蛇”的Android版本(这是一个很棒的游戏)。 However, we seem to be getting a delay on the touch inputs just big enough to make a difference. 但是,我们似乎在触摸输入方面的延迟就足够大了,可以有所作为。 (The snake might turn a step later than you intended) After getting almost used to the android version, the iPhone version feels like a blessing. (这条蛇可能比您预期的晚了一步)在几乎习惯了android版本之后,iPhone版本感觉很幸运。

The question is whether there is a delay from androids side in calling the onTouchEvent() method? 问题是,在调用onTouchEvent()方法时,android方面是否存在延迟? (I'm using Samsung Galaxy S2) If so, is there any neat tricks to circumvent the issue?(Even if it means using the NDK) (我正在使用Samsung Galaxy S2)如果是,是否有任何巧妙的方法来解决此问题?(即使这意味着使用NDK)

This is the current implementation touch handling: 这是当前实现的触摸处理:

public boolean onTouchEvent(final MotionEvent event) {      
    int action = event.getAction();
    if(action == MotionEvent.ACTION_DOWN || 
            (action & MotionEvent.ACTION_MASK) == MotionEvent.ACTION_POINTER_DOWN){     
        Run run = GameVars.run;

        if(run != null){
            if(run.paused){
                run.paused = false;
            }else{
                if(event.getX(action >> MotionEvent.ACTION_POINTER_ID_SHIFT) < GameVars.width/2){
                    run.addTap(-1);

                }else{
                    run.addTap(1);

                }
            }
        }
        return true;
    }
    return false;
}

This is the addTap method: 这是addTap方法:

public void addTap(int newDir) {
    if(newDir == -tapDir){
        taps++;
        tapDir *= -1;
    }
}

And then every frame, taps is checked, and if it's greater than zero (and we're allowed to turn atm) we turn. 然后检查每一帧的抽头,如果它大于零(允许我们打开atm),我们将旋转。

It all works fine and is reliable, but there is a consistent delay (maybe 2-3 frames, which is enough to make the iPhone snake seriously more maneuverable). 一切正常,可靠,但是会有一个一致的延迟(也许是2-3帧,足以使iPhone蛇严重地具有操纵性)。

Any feedback is apprieciated! 任何反馈表示赞赏!

I've come to realize that it is indeed a delay from the side of the device. 我已经意识到这确实是从设备侧面的延迟。 It is not big but it is there, and it varies quite a lot between different android phones. 它不大,但确实存在,并且在不同的Android手机之间差异很大。 My Galaxy S2 has a bigger delay than most of the newer HTC models I've tested. 与我测试过的大多数最新HTC机型相比,我的Galaxy S2的延迟更大。 I've also made new project where a touch switch background / play a sound (I know that has an additional delay) etc. And I can't seem to find anything that circumvents the problem. 我还创建了一个新项目,其中触摸开关背景/播放声音(我知道这会带来额外的延迟)等。而且我似乎找不到任何能解决问题的方法。

I think this is Android or device issue. 我认为这是Android或设备问题。 I have same problems with Galaxy Tab 10.1, Galaxy Note and Galaxy S II devices when develop NDK and GLES based apps, ie in contrast to iOS devices, lags between actual touch and receiving event on all "galaxies" depends to current gpu/cpu utilization, even if main (event processing) thread is do nothing, except processing theses events and all rendering and other things are performed in separated thread... 开发基于NDK和GLES的应用程序时,Galaxy Tab 10.1,Galaxy Note和Galaxy S II设备存在相同的问题,即与iOS设备相比,所有“星系”上实际触摸和接收事件之间的滞后取决于当前的GPU / CPU使用率,即使主(事件处理)线程什么也不做,除了处理这些事件以及所有渲染和其他操作都在单独的线程中执行...

This is not a solution, but a direct answer to you question: no, there is no delay on android's side for a touch event. 这不是解决方案,而是对您的问题的直接答案:不,触摸事件在android方面没有延迟。

Somewhere in your code is the source of the problem. 问题出在代码中的某个地方。 It could be a variety of things. 可能是各种各样的事情。

For example, java is a multi-threaded language, so code does not necessarily run in a linear fashion. 例如,java是一种多线程语言,因此代码不一定以线性方式运行。 The Android UI thread will run on a different thread than your Surface, so they may not be syncing the way you expect. Android UI线程将在与Surface不同的线程上运行,因此它们可能未按照您期望的方式进行同步。

Another possibility is that your controls are not getting touched. 另一种可能性是您的控件没有被触摸。 Are the touch areas small? 接触面积小吗? iPhone has an excellent and responsive touch screen, but not all android devices are built to the same quality. iPhone具有出色的响应式触摸屏,但并非所有android设备都具有相同的质量。

I wish I knew the specific cause, but i can let you safely eliminate the "its just freakin android" possibility. 我希望我知道具体的原因,但我可以让您安全地消除“它只是机器人的freakin”的可能性。 There is no delay caused by the OS. 没有由操作系统引起的延迟。

Make sure the garbage collector doesn't run when you are handling the click event. 在处理click事件时,请确保垃圾收集器没有运行。 If so, disable garbage collecting when your game is running. 如果是这样,请在游戏运行时禁用垃圾收集。 Otherwise, you are doing something expensive in your ui cycle that is causing stutter. 否则,您会在ui周期中做一些昂贵的事情,从而导致结巴。 Profiling your app should reveal the problem. 对您的应用进行性能分析应该可以发现问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM