【发布时间】:2017-02-19 14:46:31
【问题描述】:
必须有一个非常简单的解决方案,因为我尝试过使用 setInterval(),但似乎无法得到我想要的结果......
我有这个 globalTick()
function globalTick(){
ctx.clearRect(0,0,800,800)
if(target){
target.tick();
}
// Move arrows
for(var i = 0; i < spriteList.length; i++){
spriteList[i].tick()
}
window.requestAnimationFrame(globalTick);
}
document.onkeyup = function(e){
// Change gamestate
if(e.which == 13){
$("#state"+gameState).hide()
gameState ++
if(gameState == 6){
// Affect game variables
player = new Player(0);
target = new Target(targetX, targetY)
for(var i = 0; i < player.calculateNotes(); i++){
timerID = setInterval(spawnArrows(), 3000)
}
clearInterval(timerID)
}
// ...
spawnArrows = function(){
// Rabdomize arrows (15 for now)
var dirCode = Math.floor((Math.random() * 8));
spriteList.push(new Arrow(dirCode))
//soundsSequence.push(soundsList[dirCode])
}
然后我的精灵上有这个 tick() 方法,在这种情况下,是一个箭头对象
class Arrow{
constructor(dirCode){
this.dirCode = dirCode;
this.speedX = 1
this.speedY = 1
switch(this.dirCode){
// ...
}
tick(){
ctx.fillStyle = "black"
ctx.fillRect(this.x, this.y, spriteSize, spriteSize)
switch(this.dirCode){
case 0:
this.x += this.speedX
break
case 1:
this.x += this.speedX
this.y -= this.speedY
break
// ...
我已经为你们省去了变量声明。
我想要的是将每个新箭头延迟到另一个对象中存在的设定时间,例如 3 秒。是否有可能在 requestAnimationFrame 每秒调用 60 次左右的 globalTick() 内减速?理想情况下在纯 JS 中,除非 JQuery 是唯一的方法......
非常感谢,希望这足够清楚!
【问题讨论】:
-
看这里stackoverflow.com/questions/42318765/…我解释了如何用承诺制作瀑布
标签: javascript jquery requestanimationframe