Javascript 动态FPS的播放器移动计算(每秒帧数)
如果游戏以177FPS或22FPS的速度运行,如何计算玩家的移动?我试图用Javascript实现这一点:Javascript 动态FPS的播放器移动计算(每秒帧数),javascript,game-engine,Javascript,Game Engine,如果游戏以177FPS或22FPS的速度运行,如何计算玩家的移动?我试图用Javascript实现这一点: setInterval(update, 0); function update() { player_x++; } 问题是播放器是否会根据帧速率移动得更快/更慢。我该如何解决这个问题呢?试着使用计时器功能,这样玩家可以根据时间移动而不改变帧…试着使用计时器功能,这样玩家可以根据时间移动而不改变帧…距离等于速度常数和时间。根据以Hz为单位的帧速率,时间t=1/帧速率将有所不同
setInterval(update, 0);
function update() {
player_x++;
}
问题是播放器是否会根据帧速率移动得更快/更慢。我该如何解决这个问题呢?试着使用计时器功能,这样玩家可以根据时间移动而不改变帧…试着使用计时器功能,这样玩家可以根据时间移动而不改变帧…距离等于速度常数和时间。根据以Hz为单位的帧速率,时间t=1/帧速率将有所不同 要将其转化为代码:测量后续帧之间的时间,并使用该时间计算距离 有时候,更好的办法是在JavaScript中设置一个后台线程,您可以按照CezarisLT的建议使用setInterval并使用常量时间。但是,在实践中,您仍然需要测量后续调用之间的时间,因为setInterval不能保证在计划的时间内准确运行长时间运行的函数,CPU使用率高 顺便说一句,您的代码过于冗长,无法编译:
setInterval(update, 0)
function update() {
player_x++;
}
距离等于速度常数和时间。根据以Hz为单位的帧速率,时间t=1/帧速率将有所不同 要将其转化为代码:测量后续帧之间的时间,并使用该时间计算距离 有时候,更好的办法是在JavaScript中设置一个后台线程,您可以按照CezarisLT的建议使用setInterval并使用常量时间。但是,在实践中,您仍然需要测量后续调用之间的时间,因为setInterval不能保证在计划的时间内准确运行长时间运行的函数,CPU使用率高 顺便说一句,您的代码过于冗长,无法编译:
setInterval(update, 0)
function update() {
player_x++;
}
我强烈建议使用when-available,这将使您在现代浏览器中获得尽可能最好的帧速率,并在不可用时设置超时。然后根据动画开始后的时间确定玩家的位置:
// Anonymous function used to make variables declared inside it not global
// This avoids conflicts in case variables with the same names are used
// in other scripts
(function(){
// This checks for the requestAnimationFrame in each browser and store the first
// it finds into requestAnimationFrame. If none are found it uses setTimeout
// Attempting 60 frames per second.
// It's taken from Paul Irish's blog: http://paulirish.com/2011/requestanimationframe-for-smart-animating/
// and you can use it as is
var requestAnimationFrame = (function(){
return window.requestAnimationFrame ||
window.webkitRequestAnimationFrame ||
window.mozRequestAnimationFrame ||
window.oRequestAnimationFrame ||
window.msRequestAnimationFrame ||
function(callback){
setTimeout(function(){ callback((new Date).getTime()); }, 1000/60);
};
})();
var speed = 60; // pixels per second
// This is used so that we only divide by 1000 once instead of every frame
var calc = speed/1000;
// Store the current time in start_time
var start_time = (new Date).getTime();
// This function is self invoking. It is wrapped in parenthesis and called immediately
// time is passed in by requestAnimationFrame
(function draw(time){
// Calculate the new x position based on the current time
var pos = (time - start_time)*calc;
// Update the player's position
// In my JSFiddle I update the style.left of a div
player_x = pos;
// Now that this frame is done request another frame.
// draw will be called again before that frame is rendered
requestAnimationFrame(draw);
})();
})();
我强烈建议使用when-available,这将使您在现代浏览器中获得尽可能最好的帧速率,并在不可用时设置超时。然后根据动画开始后的时间确定玩家的位置:
// Anonymous function used to make variables declared inside it not global
// This avoids conflicts in case variables with the same names are used
// in other scripts
(function(){
// This checks for the requestAnimationFrame in each browser and store the first
// it finds into requestAnimationFrame. If none are found it uses setTimeout
// Attempting 60 frames per second.
// It's taken from Paul Irish's blog: http://paulirish.com/2011/requestanimationframe-for-smart-animating/
// and you can use it as is
var requestAnimationFrame = (function(){
return window.requestAnimationFrame ||
window.webkitRequestAnimationFrame ||
window.mozRequestAnimationFrame ||
window.oRequestAnimationFrame ||
window.msRequestAnimationFrame ||
function(callback){
setTimeout(function(){ callback((new Date).getTime()); }, 1000/60);
};
})();
var speed = 60; // pixels per second
// This is used so that we only divide by 1000 once instead of every frame
var calc = speed/1000;
// Store the current time in start_time
var start_time = (new Date).getTime();
// This function is self invoking. It is wrapped in parenthesis and called immediately
// time is passed in by requestAnimationFrame
(function draw(time){
// Calculate the new x position based on the current time
var pos = (time - start_time)*calc;
// Update the player's position
// In my JSFiddle I update the style.left of a div
player_x = pos;
// Now that this frame is done request another frame.
// draw will be called again before that frame is rendered
requestAnimationFrame(draw);
})();
})();
我应该提到这一点,但我不想要一个固定的帧速率,因为据我所知,这就是定时器函数所做的。我应该提到这一点,但我不想要一个固定的帧速率,因为据我所知,这就是定时器函数所做的。这看起来像是我在寻找的,但这能成为一个函数吗?有很多annonimius函数,我不确定这里到底发生了什么。您能基本上简化一下吗?@CezarisLT我会在整个过程中添加一些注释,使其更易于理解。@CezarisLT让我知道我添加的内容是否有用。@paulp-r-o是的,非常感谢,注释很有用。我得把它改成我的项目。起初,我有一个函数,尽可能多的运行,这是在一个设定的时间间隔。在这里面,我将运行更新功能,这将移动我的播放器。这有点不同。如果你能看看这个JSFidle并尝试在requestAnimationFrame中实现重力,那就太好了,我真的不知道怎么做。先谢谢你。这似乎是我正在寻找的,但这可以成为一个函数吗?有很多annonimius函数,我不确定这里到底发生了什么。您能基本上简化一下吗?@CezarisLT我会在整个过程中添加一些注释,使其更易于理解。@CezarisLT让我知道我添加的内容是否有用。@paulp-r-o是的,非常感谢,注释很有用。我得把它改成我的项目。起初,我有一个函数,尽可能多的运行,这是在一个设定的时间间隔。在这里面,我将运行更新功能,这将移动我的播放器。这有点不同。如果你能看看这个JSFidle并尝试在requestAnimationFrame中实现重力,那就太好了,我真的不知道怎么做。先谢谢你。