Android 游戏的计时器和触摸事件(移动)处理方法
我正在做一个带有游戏循环和主动渲染的游戏(使用 SurfaceView 和 SurfaceHolder.Callback)。
我想知道如何使其与定时事件和触摸事件管理一起工作。 定时事件就像每 xx ms 发生一些事情,每个 yy ms 发生不同的事情。 触摸管理是在向下、向上和移动时更新游戏和屏幕。
我已经做了一些尝试,但它看起来有点尴尬,而且游戏的表现不是很好。
一方面,我有一个经典的游戏循环(在单独的线程中): 在运行更新、渲染、绘制、睡眠
时现在我不太清楚如何使用计时器循环来完成这项工作。我使用处理程序方法 - 创建扩展处理程序的类,在handleMessage中执行它们必须执行的操作,然后使用sendMessageDelayed调用它们自己。
他们只是更新游戏状态。结果变化的渲染将在 gameLoop 中完成。
这种方法需要同步来更新游戏循环,而不干扰计时器的更新。
而且 - 除此之外,我需要处理触摸事件 - 向下、向上和移动! 我在这里的方法是,就像在计时器中一样,只需更新游戏状态,并将其留给循环来正确处理和显示。
我不知道我是否走在完全错误的道路上,对Androd来说是菜鸟,并且让它就像我认为的“可以工作”一样。
例如,我不确定是否有必要在单独的线程中进行游戏循环。我认为这是根据一定的帧率显示触摸处理(移动)的结果。
另一方面,我可以离开循环,只告诉在每次计时器更新后以及每次触摸处理后进行绘制,但这也效果不佳。
问题是这种游戏的一般方法是什么? (定时循环/触摸事件(移动)必须连续渲染到屏幕上)。
提前致谢。
I'm doing a game with game loop and active rendering (using SurfaceView and SurfaceHolder.Callback).
I'm wondering how to make that work together with timed events and touch event management.
The timed events are like each xx ms something happens, each yy ms something different happens.
Touch management is update game and screen when down, up, and move.
I have made a few tries, but it looks somehow awkward and the game isnt performing very well..
On one side I have a classic game loop (in separate thread):
while running update, render, draw, sleep a bit
Now I didn't know well how to make this work with timer loops. I used handler approach - make classes which extend handler, do what they have to do in handleMessage, and call then themselves with sendMessageDelayed.
They just update game state. The rendering of the resulting changes would be done in the gameLoop.
This approach needs synchronization for update of gameloop not intefering with update of the timers.
And - besides of this I need to handle touch events - down, up, and move!
My approach here was, like in the timers, just update game state, and leave it to the loop to handle and display correctly.
I don't know if I'm maybe on the completly wrong path, noob to Androd, and made it just like i thought "could work".
For example I'm not sure if its necessary to make a game loop in a separate thread. I thought it was, to show the results of touch handling (move) according to certain framerate.
On the other side I could leave the loop and just tell to draw after each timer update, and after each touch handling, but this doesnt work well either.
The question is what's the general approach for this kind of game? (timed loops / touch event (move) which has to render to screen continuosly).
Thanks in advance.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您读过在 Android 游戏中使用输入管道?它为您处理输入的问题提供了解决方案。
Have you read Using Input Pipelines In Your Android Game? It presents a solution to your problem for handling input.