请检查此link。下面的代码来自这个链接。我不知道为什么它说我们必须使用线程睡眠来限制触摸事件的数量。我有一个表面视图和游戏阅读器,可以处理触摸事件推送的事件。这是睡眠所必需的吗?或者我是否应该通过控制帧率来处理gamethread中的睡眠。
try {
Thread.sleep(16);
} catch (InterruptedException e) {
}
return true;
} 完整代码
@Override
public boolean onTouchEvent(MotionEvent event) {
// we only care about down actions in this game.
try {
// history first
int hist = event.getHistorySize();
if (hist > 0) {
// add from oldest to newest
for (int i = 0; i < hist; i++) {
InputObject input = inputObjectPool.take();
input.useEventHistory(event, i);
gameThread.feedInput(input);
}
}
// current last
InputObject input = inputObjectPool.take();
input.useEvent(event);
gameThread.feedInput(input);
} catch (InterruptedException e) {
}
// don't allow more than 60 motion events per second
try {
Thread.sleep(16);
} catch (InterruptedException e) {
}
return true;
}发布于 2017-02-24 19:06:51
你的链接指向2009年发表的一篇文章,你真的应该试着找到一篇更新的文章。
代码本身看起来是错误的,作者解释说,他的代码旨在避免阻塞UI线程,但随后在onTouchEvent()中调用Thread.sleep(16),然后从UI线程调用并阻塞它。
下面这个例子怎么样?我还没有详细阅读过,但代码看起来好多了:https://www.codeproject.com/Articles/827608/Android-Basic-Game-Loop
https://stackoverflow.com/questions/42436303
复制相似问题