简单的Android游戏中的GameLoopThread,我如何执行并行线程?
我正在开发一个Android游戏,我需要一些关于多线程的帮助,因为我正在考虑它。我附加了一张图片,然后我可以解释我需要什么。 这只鸟在我的游戏中从左到右、从右到左完美地移动,也像使用的两个图像一样移动她的羽毛,但我有时会运行代码,这将开始产生鸡蛋,例如鸟会飞行 4 分钟,然后每 20 秒它就会掉落鸡蛋,现在鸡蛋将从上到下移动,我认为,将使用另一个游戏线程,我为其使用了 GameLoopThread 对象,SurfaceHolder 持有表面, 我使用过此代码 http://www.edu4java.com/androidgame/androidgame3.html 我可以设定20秒的条件来给鸡蛋,但我很困惑如何为鸡蛋制作一条线,该线会从上到下移动,而另一边的鸟会继续移动。
真的提前致谢。
阿蒂夫
I am developing an android game, i need some help regarding multi threading as i am considering it. I am attaching an image then i can explain what i need.
The bird is moving perfectly in my game from left to right and right to left, also moving her feathers as two images used, but i run the code at some time which will start to give egg, forexample bird will fly for 4 minutes and after every 20 seconds it will drop the egg, now egg will move from top to bottom, and what i think, another game thread will be used, i have used GameLoopThread object for it and SurfaceHolder holds the surface,
I have used this code http://www.edu4java.com/androidgame/androidgame3.html
I can make the condition of 20 seconds to give egg, but i am confused how to make a thread for an egg which will move from top to bottom and on other-side bird will be keep moving.
Really thanks in advance.
Atif
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您只需要一个游戏线程,不要使用两个,它会让事情变得更加复杂(您需要同步所有内容)。
通常游戏线程会执行以下步骤(并永远循环它们):
所以,您需要做的是有一个列表精灵(鸟、蛋)。您可以将它们保存在一个数组中。然后在tick()中,更新它们每个的位置。
精灵列表将是动态的,当然一开始你只有一个精灵,那就是小鸟。创建鸡蛋后,只需将其添加到精灵数组中,这样从那时起它也会被处理。
You need only one game thread, don't use two, it will make things just more complicated (you need to synchronize everything).
Usually a game thread does the following steps (and loops them forever):
So, what you need to do, is have a list of sprites (bird, egg). You can keep them in an array. Then in tick(), you update the position of each of them.
The sprite list will be dynamic, at the beginning of course you will have only one sprite, the bird. When the egg is created, just add it to the sprite array, so from that point on it will be handled as well.