【发布时间】:2014-02-05 02:27:35
【问题描述】:
所以我的问题是我想插入一个自定义动画,但我不想破坏我的游戏循环。
我的初始游戏循环在这里说明:
function init(){
if(!gameOver){
if(resetInterval>-1) clearInterval(resetInterval);
createBlock();
resetInterval = setInterval(moveDownCheck,gameSpeed);
}
}
我的游戏是一个类似俄罗斯方块的游戏,只是我没有掉落俄罗斯方块,而是掉落 2x1 不同颜色的方块。 moveDownCheck 方法检查我的 2x1 块下是否有任何块,然后将其删除 1 行。由于 2x1 块已连接,因此这工作正常,直到我有一个块在下面没有块的情况下悬挂。我想插入一个大约需要一秒钟的放置动画,并以相同的gameSpeed 增量放置悬挂块。
这是我失败的尝试:
function moveFallingDown(){
fbDownFlag = false;
clearInterval(resetInterval);
fbInterval = setInterval(function(){
fallingBlock.row++;
console.log("Dropped One Row");
},gameSpeed);
while(landscape[fallingBlock.row+1][fallingBlock.col]==0){
console.log("Waiting to Drop Falling Block");
}
clearInterval(fbInterval);
resetInterval = setInterval(moveDownCheck,gameSpeed);
}
我在这里尝试等待function(){fallingBlock.row++;},但我的游戏崩溃了,在控制台中"Dropped One Row" 而"Waiting to Drop Falling Block" 将显示数千次。
我想我不应该在这里使用 while 循环,但我能想到的唯一其他解决方案是对我的设计进行彻底的返工,或者嵌套 setInterval 方法,这会让我的头疼得厉害。
【问题讨论】: