j2me“回合制”游戏循环 游戏
编辑:这对我来说更有意义,因为我已经远离了代码,感谢您的帮助。
前几天刚刚通过 Coding Horror 发现了堆栈溢出,它看起来棒极了。 我想我会向社区询问我目前正在尝试解决的问题。
我正在使用 j2me 为 midp 2.0 手机开发一款类似 roguelike 的游戏。 该项目仍处于开发的基本阶段,我正在弄清楚它将如何运作。 我目前所坚持的部分与线程有关。
该游戏有一个自定义的 HaxCanvas 类,它扩展了 GameCanvas 并实现了可运行的。 它的 run 方法调用 repaint(),然后休眠 50 毫秒,导致帧速率为 20 FPS。 这使我能够编写游戏的其余部分,而不必在各处重新绘制,并且应该使以后的动画和效果更容易完成。 (至少在理论上)。
游戏流程由 GameManager 类控制,该类循环遍历地图上的所有 NPC,轮流执行,直到轮到玩家。 此时,我需要获得输入以允许玩家四处移动和/或攻击事物。 我最初是在 HaxCanvas
的 keyPressed
方法中调用 gameManager.runUntilHeroTurn()
。 然而,在阅读了 j2me 系统线程之后,我意识到将一个可能在回调中运行一段时间的方法放在回调中是一个坏主意。 但是,我必须使用 keyPressed 来进行输入处理,因为我需要访问数字键,而 getKeyStates() 不支持此功能。
到目前为止,我尝试将游戏循环放入其自己的线程中,但结果却导致了灾难。 游戏运行几轮后,会出现一个奇怪的“未捕获的 ArrayIndexOutOfBoundsException”,没有堆栈跟踪。
所以我想我的问题是这样的:
对于j2me中的“回合制”游戏,实现游戏循环的最佳方法是什么,仅在轮到玩家时才允许输入处理?
Edit: This makes alot more sense to me now that i've taken a step away from the code, thanks for the help.
Just found stack overflow the other day through Coding Horror and it looks awesome. Figure that i'd ask the community about a problem i'm currently trying to work out.
I'm developing a roguelike sortof game using j2me for midp 2.0 phones. The project is still in the basic stages of development as I figure out how it's going to work. The part i'm currently stuck on has to do with threading.
The game has a custom HaxCanvas
class which extends GameCanvas and Implements runnable. It's run method calls repaint() and then sleeps for 50 ms, resulting in a frame rate of 20 FPS. This allows me to write the rest of the game without having to put repaint everywhere and should make animations and effects easier to do later on. (at least in theory).
The flow of the game is controlled by a GameManager class, which loops through all the NPC's on the map, taking their turns, until it's the player's turn. At this point I need to get input to allow the player to move around and/or attack things. I originally was calling gameManager.runUntilHeroTurn()
in the keyPressed
method of my HaxCanvas
. However after reading up on j2me system threads I realized that putting a method with the potential to run for a while in a callback is a bad idea. However I must used keyPressed to do input handeling, since i need access to the number keys, and getKeyStates()
does not support this.
Sofar my attempts to put my gameloop in it's own thread have resulted in disaster. A strange "uncaught ArrayIndexOutOfBoundsException" with no stack trace shows up after the game has run for several turns .
So i suppose my question is this:
For a "turn based" game in j2me, what's the best way to implement the game loop, allowing for input handeling only when it's the player's turn?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
虽然不是 j2me 专门应该捕获用户输入,但一般策略是将输入排队,直到处理输入的时间为止。
这样您甚至可以编写脚本输入以进行调试。
所以你不需要一个新线程。 每次用户按下按键时,您都会将它们存储在缓冲区中,然后在必要时处理缓冲区的内容。 如果玩家缓冲区没有输入,管理器应该跳过所有游戏,制作动画,然后重新开始(因为游戏不是动作游戏)。
Although not j2me specifically you should capture user input, the general strategy is to queue the input it until its time to process the input.
This way you can even script input for debug purposes.
So you don't need a new thread. Each time the user presses key you store them in a buffer, and then process the contents of the buffer when necessary. If the player buffer has no input, the manager should skip all gameplay, do animations and then start over (since the game is not an action game).
我会避免游戏逻辑的线程化,因为 J2ME 线程化(当然取决于制造商)并不能很好地共享有限的资源。 当线程执行繁重的处理时,您经常会看到暂停。 我只会推荐用于加载或网络连接功能的线程,因为在这种情况下,您只需向用户提供基本的“正在加载...”反馈。
为了解决这个问题,我不会使用子循环来更新一帧中的每个 AI。 我会在运行函数中执行类似以下操作:
您希望避免在一帧中更新所有内容,因为 1) 更新可能很慢,导致用户无法立即获得视觉反馈 2) 您无法为每个单独的 NPC 设置动画,因为他们采取行动。 通过此设置,您可以拥有 NPC 状态、NPC_DECIDE_MOVE 和 NPC_ANIMATING,这将允许您进一步控制 NPC 正在执行的操作。 NPC_ANIMATING 基本上会让游戏处于等待动画发生的状态,避免在动画完成之前进行任何进一步的处理。 然后它就可以进入下一个 NPC 的回合。
另外,我只需要一个 gameManager.update() 和 gameManager.paint(g) (paint 将从 Paint 中调用)来处理所有事情并保持 run 方法的精简。
最后,你研究过flushGraphics()吗? 使用 GameCanvas,您通常创建一个 Graphics 对象,将所有内容绘制到该对象上,然后调用lushGraphics(),然后等待。 您提到的方法是 Canvas 类处理它的方法。 只是想我会提到这一点并发布一个链接:
游戏画布基础知识
I would avoid threading for the game logic as J2ME threading, depending on manufacturer of course, does not do a great job of sharing the limited resources. You will often see pauses while a thread does heavy processing. I would only recommend threads for loading or network connectivity features as in this case you will just be giving the user basic "Loading..." feedback.
To handle this, I would not have sub-loops to update each of the AI in one frame. I would do something like following in the run function:
You want to avoid having everything updated in one frame as 1) the updating might be slow, resulting in no immediate visual feedback for the user 2) you can't animate each individual NPC as they make their action. With this setup you could have NPC states, NPC_DECIDE_MOVE and NPC_ANIMATING, that would allow you further control of what the NPC is doing. NPC_ANIMATING would basically put the game in a waiting state for the animation to take place, avoiding any further processing until the animation is complete. Then it could move on to the next NPC's turn.
Also, I would just have a gameManager.update() and gameManager.paint(g) (paint would be called from paint) that would handle everything and keep the run method thin.
Finally, did you look into flushGraphics()? With the GameCanvas you usually create a Graphics object, draw everything to that and then call flushGraphics(), then wait. The method you mention is the way of tackling it for the Canvas class. Just thought I would mention this and post a link:
Game Canvas Basics