0

基本上我写了一个函数:

函数动画(){
    设置超时(
        功能(){
        requestAnimationFrame(动画);
        if (player.currentFrame == player.frames) {
            播放器.currentFrame = 0;
            } 别的 {
            player.currentFrame++;
            }
        }
    , 1000 / 15);
}

我正在尝试用这段代码调用它 animation(fps);

我试着让它像这样:

函数动画(fps){
    设置超时(
        功能(){
        requestAnimationFrame(动画);
        if (player.currentFrame == player.frames) {
            播放器.currentFrame = 0;
            } 别的 {
            player.currentFrame++;
            }
        }
    , 1000 / fps);
}

并试图用它来调用它,animation(30) 但它没有用。我试过这样的搜索主题,但没有一个是我想要的。

任何帮助,将不胜感激!:)

4

2 回答 2

1

setTimeout在时间过去后只会调用该函数一次,因此除非您animation再次使用调用,否则fps它只会运行一次并完成。您可以使用每毫秒setInterval调用一次函数。1000/fps

var frame = 0;
animation(20);
function animation(fps){
    console.log(fps)
    setInterval(
        function(){
            console.log("A");
            document.getElementById("sp1").innerHTML = frame;
            frame++;
        }
    , 1000 / fps);
}

小提琴示例:http: //jsfiddle.net/Up4Cq/

或者如果你想使用setTimeout,你需要animation再次调用该函数:

function animation(fps){
    console.log(fps)
    setTimeout(
        function(){
            console.log("A");
            document.getElementById("sp1").innerHTML = frame;
            frame++;
           animation(fps);
        }
    , 1000 / fps);
}

这是 setTimeout 的小提琴:http: //jsfiddle.net/Up4Cq/1/

于 2013-04-04T12:18:26.927 回答
1

你试过这个语法吗

function animation(fbs){
        requestAnimationFrame(animation); 
        if (player.currentFrame == player.frames) {
            player.currentFrame = 0;
        } else {
            player.currentFrame++;
        }        
       setTimeout(function(){animation(fbs) }, 1000 / fbs);
}

我将代码从更改setTimeout('animation(fbs)', 1000 / fbs);setTimeout(function(){animation(fbs) }, 1000 / fbs);

于 2013-04-04T12:27:19.523 回答