我有一个 Sprite 渲染器,它告诉我的游戏引擎如何渲染 Sprite 。此类中的 update 方法每秒被调用大约 120
次。以这样的速度运行 Sprite 表太快了。
在我的 Sprite 类中,我有一个名为duration
的属性,它告诉渲染器 Sprite 应该播放多少秒。一旦到达最后一帧,就应该重新开始。
我不太确定如何使用每秒运行 120
次的 update
以及应持续 x
的 Sprite 表来计算此值code> 秒直到重新开始。
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;
}
}
}
}
}
最佳答案
您可以实现一个控制帧时间的时间变量。 该变量是一个 float ,一旦它变得足够大,您就可以执行下一帧并重置该变量。
我从未做过任何类型脚本,但这可能有用。它至少会让您了解我在说什么。
如果更新每秒运行 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;
}
}
}
}
}
关于javascript - 在 Canvas 上播放 Sprite 表的速度比帧速率慢,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39796299/