问题标签 [game-loop]

For questions regarding programming in ECMAScript (JavaScript/JS) and its various dialects/implementations (excluding ActionScript). Note JavaScript is NOT the same as Java! Please include all relevant tags on your question; e.g., [node.js], [jquery], [json], [reactjs], [angular], [ember.js], [vue.js], [typescript], [svelte], etc.

0 投票
4 回答
2016 浏览

scala - 游戏循环的功能替代品

我刚开始使用 Scala 并正在尝试一个小玩具程序 - 在本例中是基于文本的井字游戏。我根据我对 scala 的了解编写了一个工作版本,但注意到它主要是命令式的,而且我的类是可变的。

我正在经历并尝试实现一些功能性习语,并设法至少使代表游戏状态的类不可变。但是,我剩下一个类负责执行依赖于可变状态和命令式循环的游戏循环,如下所示:

在这个循环中对我正在做的事情进行编程的更惯用的方法是什么?

完整的源代码在这里https://github.com/whaley/TicTacToe-in-Scala/tree/master/src/main/scala/com/jasonwhaley/tictactoe

0 投票
2 回答
1909 浏览

java - 更新 GUI 会产生闪烁效果

我目前有一个 JFrame,在它的内容窗格上,我以每秒 60 帧的速度从游戏循环中绘制图像。这是正常的,但在右侧,我现在有更多的Swing Elements,我想在选择内容窗格的某些部分时显示一些信息。该部分是静态 GUI,不使用游戏循环。

我正在以这种方式更新它:

(此方法仅在玩家实际移动时调用,因此仅调用一次 - 而不是每秒 60 次)

我注意到,当从游戏循环中调用这个 update() 方法时,我得到了闪烁的效果。我认为这是因为更新 UI 需要一些时间,所以我决定将它放在一个新线程中。这减少了闪烁,但没有解决它。

接下来,我决定给新线程低优先级,因为每秒重绘 60 次的屏幕部分更为重要。这再次减少了闪烁,但它仍然发生。然后,我决定Thread.sleep(150);在调用 update() 方法之前在新线程中使用,这完全解决了我系统上的闪烁效果。

但是,在其他系统上运行它时,它仍然会发生。不像以前那么频繁(可能每 20 秒一次),但它仍然很烦人。显然,仅在另一个线程中更新 UI 并不能解决问题。

任何想法如何完全消除闪烁?

0 投票
2 回答
1617 浏览

iphone - String compare vs Class compare in objective-C

I'm writing an objective-C game and I'm at the stage where i should start optimising some of my code in the game loops.
I have an extensive amount of class compare methods used,

if ([obj isMemberOfClass:[SomeClass class]])

etc.
I heard this sort of checking is quite expensive, because I choose only to have 1 array populated with multiple classes, I need some sort of class check.
I was thinking perhaps that adding a property to NSObject subclassing NSObject to contain a string property, that during initialisation i would make equal to the class name of that particular subclass. Then simply doing a

if ([obj.klass isEqualTo:@"SomeClass"])

Would this be beneficial?
I'm trying to keep as much dynamic coding out of the game loops as possible.

Thanks!

0 投票
2 回答
1160 浏览

android - android游戏循环睡眠

我正在开发一款适用于 Android 的迷你游戏。我将此代码用于游戏循环。

RefreshHandler 类来自 SnakeView 类示例。

代码工作正常。我有一个新游戏按钮的活动。当我第一次进入活动时,它是有效的。但是,如果我退出游戏并再次进入速度会更快,这是我不会或不理解的事情。如果我退出游戏并使用“新游戏”按钮再次进入,速度会更快。

谁能帮我这个 ?

0 投票
1 回答
1384 浏览

android - 游戏循环中onResume后的Android-null指针异常

我正在开发一款安卓游戏。我面临与这里相同的问题

我尝试了等待并通知所有解决方案,如图所示。但是一旦应用程序恢复,我就会得到空指针异常。这是日志猫信息。

onSurfaceCreated 和 onSurfaceDestroyed 里面的代码

我的 GameThread 类中的代码

}

活动类 onPause

如何在onResume中保持游戏状态?谢谢

0 投票
1 回答
1035 浏览

android - Android gameloop,更新和绘制是否分开?

我只是拿起android开发来制作游戏。以前接触过,但只学习了基础知识。我有点困惑如何设置主循环。我已经进入 XNA (C#) 并且我喜欢分离的更新/绘制循环。

我想知道典型的 android gameloop 是如何工作的?我在网上搜索并遇到了两种方法:


让我们以游戏实体的更新为例来说明这两种方法:

现在我对线程没有任何经验,所以我不知道 XNA 如何在 xna 的屏幕后面进行更新/绘制循环。但是使用方法 1,我将不得不循环遍历所有实体两次,一次用于更新,另一次用于单独绘图。恐怕这会影响性能,但我已经在网上的示例中看到了这一点。

我错过了什么还是我是对的,方法 2 是最好的表现吗?

0 投票
2 回答
763 浏览

java - 这个小程序游戏循环有什么问题?

似乎这个小程序只会在调整窗口大小或最小化窗口时绘制和更新 DRAWS。所以小程序不会一直重绘,而只会在操作窗口时重绘。

我在这里做错了吗?

我正在关注这里介绍的游戏循环:http ://www3.ntu.edu.sg/home/ehchua/programming/java/J8d_Game_Framework.html

代码在这里:

0 投票
4 回答
1120 浏览

iphone - 创建一个 NSThread 游戏循环

我试图创建一个 NSThread 游戏循环,我有一些时间能够成功获得 57 FPS。

有时我的 fps 会上升到一些可笑的数字。

我不明白它是如何发生的。

我检查自上次循环以来的时间,如果它很快,则让线程休眠这么长时间。

这并不总是发生,它有时会逃避 if 检查速度并以循环方式快速。

任何评论都意味着很多。

我应该在哪里“打勾”?

0 投票
2 回答
721 浏览

javascript - 游戏循环内存泄漏

我正在用javascript创建一个游戏,我的gameloop每30毫秒调用一次,它会泄漏大量内存,因为任务管理器显示firefox内存使用量在大约20秒内增加了400mb。我不熟悉如何确保在 javascript 中收集内存。

好的,我注释掉了一些代码并找到了有问题的行,它

ship.ship.rotate(角度); 在注释掉那条线后,javascript 正在使用 4500K。知道为什么这会导致问题,如果没有这段代码,我怎么能旋转我的对象?

0 投票
5 回答
1904 浏览

c# - I want to force a render, but only draw as fast as possible (InvalidateVisual / CompositionTarget.Rendering)

I'm working on a real-time WPF/Silverlight (and soon WP7) visualization component and I'm looking for the best solution to force a redraw of the entire component in a Game-loop style. Redraw should be on-demand, but I don't want to back up the message pump with re-draw calls. Most of the drawing in my component is done using non-WPF primitives (e.g. Bitmap Interop, Direct2D) so my code does not use InvalidateVisual, and as a result, currently looks like this

Ok so this is great. If I call InvalidateElement lots of times I get good responsiveness. However, what I want to do is ensure I can push data to my visualization component as fast as possible but only draw when the component is able to draw, and not keep drawing to catch up with the data once the input stream completes.

No I can't override OnRender, I'm using non-WPF drawing inside WPF ;-)

Basically what I want is something like the old Invalidate() / OnPaint in WindowsForms, or better yet, a game loop in DirectX.

At the moment I get the situation where if I have an external thread that pushes data to the visualization component at a high rate then if I Stop pushing data I get another 20 seconds worth of refreshes to get through before the component stops drawing. I want to stop drawing as soon as data has gone in.

Another idea I had was to handle CompositionTarget.Rendering in the visualization component then implement some sort of rudimentary Queue to push data to and the Rendering event consumes this data as fast as it can.

In Summary

Given a WPF visualization component, V, and a datasource which pushes it data every 1ms, D, how can I ensure that no matter the datarate of D, V draws data at 30FPS (or whatever it can do) and updates itself in chunks, sort of how a game render loop does in DirectX?

When the data stops, V should redraw everything it has up to now in one go. When the data is too fast, V draws larger chunks at a time to compensate.

If you need more information I'd be happy to share it. Right now I've just posted a synopsis to gauge if there are any quick fixes but a fuller Q with code examples can be provided on request.

Best regards,