java - 在游戏线程上处理 MotionEvent

标签 java android multithreading

我正在为 Android 开发游戏并使用以下方法将在 View 上捕获的 MotionEvents 传递给游戏进行处理。

@Override
public boolean onTouchEvent(MotionEvent event){
    game.input(event);
    return true;
}

使用View#onDraw 方法更新和绘制游戏:

@Override
public void onDraw(Canvas canvas){
    game.update();
    game.draw(canvas);
    this.invalidate();
}

如您所知,android 在单独的线程上处理输入和渲染,因此可以在渲染帧时或更新逻辑后将 MotionEvent 传递给游戏。这不适合我在游戏中处理输入的方式。

我试过将事件排列在 ArrayList 中并在游戏的更新方法中处理它们。然而,正如预期的那样,这会在队列中的项目处理完后清除队列时导致并发修改异常。

对于如何使排队系统工作或任何其他方法的帮助,我将不胜感激。

最佳答案

如果我正确理解了您的问题,那是在架构方面。您需要使用 SurfaceView .探索此 Material :http://obviam.net/index.php/a-very-basic-the-game-loop-for-android/ 您需要一个示例:

enter image description here

有用的资料还在这里: View vs SurfaceView for android game

关于java - 在游戏线程上处理 MotionEvent,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33987356/

相关文章:

java - TypedQuery 无法在 where block 中处理 boolean 值

android - 错误 ENOENT : no such file or directory, 打开 'app/src/main/AndroidManifest.xml'

java - Swing:重置 ObjectOutputStream 时出现 EDT NullPointerException

javascript - 所有异步 IO Nodejs 代码都使用 libuv 吗?

objective-c - 核心数据多线程导入(重复对象)

java - 我应该使用什么来代替 com.google.appengine.repackaged*?

java.net.SocketTimeoutException : Read timed out while downloading google spreadsheet

java - 布局单元中的背景图像

安卓:UI旧界面

java - 在服务内运行加速度计会出现 "Unable to create service"异常