繁体   English   中英

在画布上播放精灵表慢于帧频

[英]Play sprite sheet on canvas slower than frame rate

我有一个精灵渲染器,它告诉我的游戏引擎如何渲染精灵。 此类中的update方法每秒被调用约120次。 以这种速度在Sprite表中运行太快。

在我的Sprite类中,我有一个名为duration的属性,该属性告诉渲染器Sprite应该播放多少秒。 一旦到达最后一帧,它应该重新开始。

我不确定如何使用每秒运行120次的update以及应该持续x秒直到重新开始的sprite表格来计算该值。

class SpriteRenderer extends Component {

    // The current frame
    public frame: number = 0;
    // The sprite reference
    public sprite: Sprite = null;

    update() {

        // Number of frames in the sprite sheet
        let frames = this.sprite.frames;
        if (frames > 0) {
            // The time in seconds the sprite sheet should play
            let duration = this.sprite.duration;

            if (/* What should go here? */) {
                this.frame++;
                if (this.frame > frames - 1) {
                    this.frame = 0;
                }
            }
        }

    }

}

您可以实现一个控制帧时间的时间变量。 该变量是一个浮点数,一旦变大,就可以进行下一帧并重置该变量。

我从未做过任何类型脚本,但这可能有用。 它至少会让您了解我在说什么。

如果更新每秒运行120次,则意味着更新每60/120秒运行一次0.5。

现在我们可以将currentTime增加0.5,并检查currentTime > sprite.duration * 60是否为我认为。 :)

实例:

class SpriteRenderer extends Component {

    // The current frame
    public frame: number = 0;
    // The sprite reference
    public sprite: Sprite = null;
    public currentTime: number = 0.0; //This is the current time.
    public updateTime: number = this.sprite.duration*60; //This is how long the update time is.
    update() {
        this.currentTime += 0.5; //Add to the current time.
        // Number of frames in the sprite sheet
        let frames = this.sprite.frames;
        if (frames > 0) {
            // The time in seconds the sprite sheet should play
            let duration = this.sprite.duration;

            if (this.currentTime > this.sprite.duration*60) { //Check if the current time is more than the wait time.
                this.currentTime = 0.0; //Reset the wait time.
                this.frame++;
                if (this.frame > frames - 1) {
                    this.frame = 0;
                }
            }
        }

    }

}

暂无
暂无

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

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