简体   繁体   English

更改更新速度而不更改循环速度?

[英]change update speed without changing loop speed?

Could someone explain me how to change my hero animation without changing setTimeout. 有人可以向我解释如何在不更改setTimeout的情况下更改英雄动画。 i Mean how to make hero animate slower than all canvas. 我的意思是如何使英雄的动画制作速度比所有画布慢。 by animate i mean changing frames 通过动画,我的意思是改变框架

function draw() {
if (hero.ready){
    ctx.clearRect (0 , 0, 500 , 500 );
    ctx.shadowColor = "rgba( 0, 0, 0, 0.1 )";
    ctx.shadowOffsetX = 15;
    ctx.shadowOffsetY = -10;
    ctx.drawImage(image[hero.image],hero.frames * hero.sizeX, 0, hero.sizeX, hero.sizeY, hero.posX, hero.posY, hero.sizeX, hero.sizeY);
}
hero.frames++;
if (hero.frames >= 2) hero.frames = 0;
setTimeout( draw, 1000 / 5 );
}

JSFIDDLE full example. JSFIDDLE的完整示例。

To clarify on the concept of 'delta time', if you were to simply increment a counter without scaling it against real time, it would increment as fast as draw is called. 为了阐明“增量时间”的概念,如果您只是简单地增加一个计数器而不对实时进行缩放,那么它将以调用draw速度增加。 However by scaling it, you are guaranteed a certain amount of frames per second. 但是,通过缩放比例,可以确保每秒一定数量的帧。 This way, you can always be sure that your animation is as slow or as fast as you want it. 这样,您始终可以确保动画的速度和速度一样快。 You can set your threshold to be for example 500 milliseconds (one frame every half a second), and so on. 您可以将阈值设置为例如500毫秒(每半秒一帧),依此类推。

var counter = 0;
function draw() {
  // deltaTime is how you plan on counting real seconds
  // against your frame ticks
  counter += deltaTime;

...

// threshold would be your delay
if (counter >= threshold)
{
  hero.frames++;
  if (hero.frames >= 2) hero.frames = 0;
  counter = 0;
}

}

setInterval( draw, 1000 / 5 );

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

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