在 canvas 上以低于帧速率的速度播放精灵 sheet
Play sprite sheet on canvas slower than frame rate
我有一个精灵渲染器,它告诉我的游戏引擎如何渲染精灵。 class 中的更新方法每秒被调用 120
次。 运行通过精灵sheet那个速度太快了。
在我的精灵 class 中,我有一个名为 duration
的 属性,它告诉渲染器精灵应该播放多少秒。一旦到达最后一帧,它应该重新开始。
我不确定如何使用每秒运行 120
次的 update
和应该持续 x
秒的精灵 sheet 来计算这个直到它重新开始。
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;
}
}
}
}
}
我有一个精灵渲染器,它告诉我的游戏引擎如何渲染精灵。 class 中的更新方法每秒被调用 120
次。 运行通过精灵sheet那个速度太快了。
在我的精灵 class 中,我有一个名为 duration
的 属性,它告诉渲染器精灵应该播放多少秒。一旦到达最后一帧,它应该重新开始。
我不确定如何使用每秒运行 120
次的 update
和应该持续 x
秒的精灵 sheet 来计算这个直到它重新开始。
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;
}
}
}
}
}