我正在使用Javascript制作游戏,在这种情况下,下面的方法应该将红色滑块在屏幕上滑动到指定的x坐标。问题在于代码执行速度很快,因此它没有出现在屏幕上,而是在屏幕上平滑滑动。我尝试使用setTimeout函数无济于事,但似乎不能减慢运动速度。我试图尽量减少代码以简化代码,但是如果您需要更多代码,请告诉我。
blob.prototype.glide = function(xf){
for(let i = this.x; i<=xf; i++){
fillCanvas("grey");
this.x++;
this.render(); //generates a red square on the map
}
}
尽管我怀疑该链接对您有很大帮助,但该网站的链接为https://worried-sleet-blue.glitch.me/。谢谢。
setTimeout(function() { ... your function content }, XXX); // XXX = number of milliseconds use to fine tune your animation
setTimeout(function() { ... your function content }, 1000/60); // this would give you about 60 frames per second
简单有效。
blob.prototype.glide = setTimeout(function(xf){
for(let i = this.x; i<=xf; i++){
fillCanvas("grey");
this.x++;
this.render(); //generates a red square on the map
}
}, 1000/60);