使用 wait() 和 notification() 暂停/恢复游戏线程 - Android
我有一个主游戏线程,但当达到目标分数时,我会启动一个名为 StageCleared 的活动,该活动基本上向用户显示其性能的统计数据,然后他们可以按继续按钮继续游戏。这应该将焦点切换回应该继续执行的正在运行的线程,从而显示游戏活动(参数在 StageCleared 执行后更新)。
有人建议我使用一个包可见对象,该对象在主游戏线程中对其自身调用 wait() ,然后从 StageCleared 对其自身调用 notification() 以便继续执行。我的第一个问题是我似乎无法声明一个包中的所有类都可以看到的包可见对象?其次,这是实现我想要做的事情的最佳方法还是有更好的方法?
非常感谢
I have a main game thread but when the target score is achieved I have an activity that is launched called StageCleared which basically displays the stats to the user of their performance and then they can press a continue button to carry on with the game. This should switch focus back to the running thread that should continue execution, and thus display the game activity (with parameters i update after StageCleared has exectued).
It was suggested I use a package visible object that calls wait() on itself in the main game thread, and then notify() on itself from StageCleared in order to continue execution. My first problem is I can't seem to declare a package visible object that can be seen by all the classes in my package? Secondly, is this the best way to achieve what I'm intending to do or is there a better way?
Many thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
要启用包可见,请将修饰符留空:
mVarname 在包内可见。
我使用运行标志来启用循环是否应该执行某些操作或只是“空闲”。我也一样,不知道这是否是一个好方法:)
To enable package visibile, leave the modifier blank:
mVarname is visible inside the package.
I work with a run flag to enable if the loop should do something or just "idle". I, too, dont know if this is a good way to do it :)
使用FutureTask和Executor,在Java API中查看。
就像,您定义操作和 FutureTask,它会等待任务完成。
比 wait() 更容易
Use FutureTask and Executor, check it out in Java API.
It's like, you define the operation and FutureTask, and it'll wait until the task is completed.
Easier than wait()