简体   繁体   English

PixiJS v4限制帧率

[英]PixiJS v4 Limiting Frame Rate

Using a spritesheet, I'm trying to figure out the best way to animate through all the frames smoothly and consistently, though I need to lower the FPS from 60 to 30. 尽管我需要将FPS从60降低到30,但我仍在尝试使用Spritesheet找出最佳方法来平滑且一致地遍历所有帧。

Trying to use AnimatedSprite always defaults to 60 fps, which makes my animation flow too fast. 尝试使用AnimatedSprite始终默认为60 fps,这会使我的动画流过快。

var frames = [];

for (var i = 0; i < maxFrames; i++){
    frames.push(PIXI.Texture.fromImage("spriteFrame_"+ i));
}

//// How to do it via AnimatedSprite
var s = new PIXI.extras.AnimatedSprite(frames);
s.play();
Stage.addChild(s);

My current way of doing it sets the sprite 's texture manually, and hackily halves the frame rate 我目前的操作方式是手动设置sprite的纹理,然后将帧速率降低一半

// in bonusSymbol.js
var curFrameNumber = 0;

function nextFrame(){
   curFrameNumber ++;

   if(curFrameNumber >= frames.length){
      curFrameNumber = 0;
   }

   sprite.texture = frames[curFrameNumber];
}

// in game.js
function gameLoop() {
    //Loop this function at 60 frames per second
    requestAnimationFrame(gameLoop);

    // Force to only update on half frames (30fps)
    if(frameCounter == 1){
        frameCounter = 0;
        return;
    }
    frameCounter++;


    bonusSym.NextFrame();
    Scene.Render()
};

Is there any alternative or better way of doing this? 有其他替代方法或更好的方法吗?

There is a property for AnimatedSprite called animationSpeed. AnimatedSprite有一个名为animationSpeed的属性。

var frames = [];

for (var i = 0; i < maxFrames; i++){
    frames.push(PIXI.Texture.fromImage("spriteFrame_"+ i));
}

//// How to do it via AnimatedSprite
var s = new PIXI.extras.AnimatedSprite(frames);
s.animationSpeed = 0.5;
s.play();
Stage.addChild(s);

Alternatively, instead of just an array of textures to give to the AnimatedSprite, you can pass through an array of FrameObject, which give both the texture and how long it should be displayed for in ms. 或者,您不仅可以传递给AnimatedSprite的纹理数组,还可以传递一个FrameObject数组,该数组既提供纹理又显示应该以ms为单位的显示时间。

var frames = [];

for (var i = 0; i < maxFrames; i++){
    frame = {
        texture: PIXI.Texture.fromImage("spriteFrame_"+ i),
        time: 33
    };
    frames.push(frame);
}

//// How to do it via AnimatedSprite
var s = new PIXI.extras.AnimatedSprite(frames);
s.play();
Stage.addChild(s);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM