Android SurfaceView 上的 2D 游戏在触摸时运行缓慢

发布于 2024-09-16 14:49:58 字数 218 浏览 1 评论 0原文

我正在使用 Android SurfaceView 构建 2D 游戏。当我触摸屏幕时,游戏动画运行缓慢。为什么,我该如何避免这种情况?

触摸事件是存根,就像 onTouchEvents(MotionEvents ev){empty }; 一样。所有游戏逻辑和图形绘制代码都位于 extendsThread 类的 run() 中。

I am building a 2D game with Android SurfaceView. When I touch the screen the game animation runs slowly. Why, and how can I avoid this?

The touch events are stubs just like onTouchEvents(MotionEvents ev){ empty };. All of the game logic and graphics draw code are in run() in an extends Thread class.

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

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

发布评论

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

评论(3

月依秋水 2024-09-23 14:49:58

让 UI 线程休眠 16-20 毫秒将防止触摸事件处理代码每秒被调用太多次。

示例:

@Override
public boolean onTouchEvent(MotionEvent event) {

 //Event handling logic

    try {
        Thread.sleep(16);
    } catch (InterruptedException e) {
        e.printStackTrace();
    }
    return true;
}

触摸事件的调度速度与 UI 线程可以读取它们的速度一样快,通过休眠线程,您可以跳过一些事件调度。 (虽然对于你的游戏逻辑或用户来说并不是一个明显的数量)

Sleeping the UI thread for 16-20ms will keep the touch event handling code from being called too many times per second.

Example:

@Override
public boolean onTouchEvent(MotionEvent event) {

 //Event handling logic

    try {
        Thread.sleep(16);
    } catch (InterruptedException e) {
        e.printStackTrace();
    }
    return true;
}

Touch events are dispatched as quickly as the UI thread can read them, by sleeping the thread you are able to skip a number of event dispatches. (Though not a noticeable amount for your game logic or the user)

坚持沉默 2024-09-23 14:49:58

您是否看过 http://www.droidnova.com 上的教程?它们是很好的入门游戏,当我玩它们时,它们运行得很快。如果它们在您的触摸设备上运行缓慢,则可能不是开发问题

Have you looked at the tutorials at http://www.droidnova.com ? They are good intro game tuts and ran very quickly when I did them. If they run slowly on your device with touch it is probably not a dev issue

や莫失莫忘 2024-09-23 14:49:58
@Override
public void run ( )
{
    while(irunning)
    {
        while(paused)
        {
            sleep(2);
            synchronized ( holder )
            {
                    canvas = holder.lockCanvas(null);
                    //updateEvents();
                    //updatePsgstate();
                    //updateRole();
                    sortImages();
                    drawBgland();
                    drawImages();
                    drawAdminicle();
                    holder.unlockCanvasAndPost(canvas);                         
            }

        }
        sleep(10);
    }

}



@Override
public boolean dispatchTouchEvent ( MotionEvent ev )
{
    try
    {
        Thread.sleep( 32 );
    }
    catch ( InterruptedException e )
    {           
        e.printStackTrace();
    }
    synchronized ( ev )
    {
        Log.i( "T" , "onTouch" );
    }
    return true;
}


@Override
public void surfaceCreated(SurfaceHolder holder) 
{               
    mThread.start();

// 如果 (hasFocus())
// {
// 返回;
// }
// 别的
// {
// requestFocusFromTouch();
// }

}

这是我使用的基本方法。
这是我的代码。有什么问题吗?

@Override
public void run ( )
{
    while(irunning)
    {
        while(paused)
        {
            sleep(2);
            synchronized ( holder )
            {
                    canvas = holder.lockCanvas(null);
                    //updateEvents();
                    //updatePsgstate();
                    //updateRole();
                    sortImages();
                    drawBgland();
                    drawImages();
                    drawAdminicle();
                    holder.unlockCanvasAndPost(canvas);                         
            }

        }
        sleep(10);
    }

}



@Override
public boolean dispatchTouchEvent ( MotionEvent ev )
{
    try
    {
        Thread.sleep( 32 );
    }
    catch ( InterruptedException e )
    {           
        e.printStackTrace();
    }
    synchronized ( ev )
    {
        Log.i( "T" , "onTouch" );
    }
    return true;
}


@Override
public void surfaceCreated(SurfaceHolder holder) 
{               
    mThread.start();

// if (hasFocus())
// {
// return;
// }
// else
// {
// requestFocusFromTouch();
// }

}

this is base method that i used.
that's my code. any problem about it ?

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