为什么我的游戏角色在不同的电脑上速度不同?

2022-01-17 00:00:00 animation javascript html5-canvas

我正在使用 Java Script 开发在线游戏.我正在使用 setInterval (movimage, 10) 方法来移动游戏角色.但是我看到游戏角色的移动速度是不一样的都是电脑.请给我建议.

I am developing an online game using Java Script. I am using setInterval (movimage, 10) method to move the game character. But I have seen that the movement speed of the game character is not same is all computer. Please suggest me.

推荐答案

你可能应该使用 requestAnimationFrame (https://developer.mozilla.org/en-US/docs/DOM/window.requestAnimationFrame).

Instead of setInterval you should probably use requestAnimationFrame (https://developer.mozilla.org/en-US/docs/DOM/window.requestAnimationFrame).

尝试比屏幕绘制速度更快地更新内容是没有意义的.您的目标是 60fps,即每帧约 16 毫秒.

There is no point trying to update something quicker than the screen can draw. You are aiming for 60fps, which is about 16ms per frame.

http://paulirish.com/2011/requestanimationframe-for-smart-animating/ 提供了有关如何执行此操作的更多信息.

http://paulirish.com/2011/requestanimationframe-for-smart-animating/ has some more info about how to do this.

浏览器支持非常好(http://caniuse.com/#feat=requestanimationframe)简而言之,除了 Android Stock 浏览器之外的所有当前浏览器.

Browser support is pretty good (http://caniuse.com/#feat=requestanimationframe) in short, all current browsers apart from the Android Stock browser.

如果您必须在 IE9 及更低版本中使用此功能,https://gist.github.com/paulirish/1579671 在模拟这些浏览器中的行为方面做得不错.(虽然说实话,我怀疑这将是你最后的担忧,特别是关于缺少 canvas...)

If you must have this working in IE9 and lower, https://gist.github.com/paulirish/1579671 does a decent job of simulating the behaviour in those browsers. (though to be honest, I suspect this will be the last of your worries, particularly in regard to the lack of canvas…)

相关文章