var ExplodeFrame = 0;
var ExplodeTimeout;
当玩家击中熔岩时,所有这一部分都会检查。有用。请参阅注释部分以调用动画
if((Player.x > Lavas[i].x && Player.x < (Lavas[i].x + Lavas[i].width)) || (Player.x + Player.width > Lavas[i].x && Player.x + Player.width < (Lavas[i].x + Lavas[i].width)))
{
if((Player.y > Lavas[i].y && Player.y < Lavas[i].y + Lavas[i].height) || (Player.y + Player.height > Lavas[i].y && Player.y + Player.height < Lavas[i].y + Lavas[i].height))
{
ExplodeTimeout = setTimeout(DrawSpawnAnimation, 200); //this part is new and might not work
Player.jumping = false;
Player.velY = 0;
Player.velX = 0;
Player.x = Player.spawnX;
Player.y = Player.spawnY;
Player.playerColour = Player.colour;
}
}
}
这种方法对我来说似乎不起作用?
function DrawSpawnAnimation()
{
ExplodeFrame++;
alert("current frame" + ExplodeFrame);
var explodeX = Player.x - 50;
var explodeY = Player.y - 50;
if(ExplodeFrame == 1)
{
ctxAnimation.drawImage(spriteSheet,0,2740,100,100,explodeX,explodeY,100,100);
}
else if(ExplodeFrame == 2)
{
ctxAnimation.drawImage(spriteSheet,100,2740,100,100,explodeX,explodeY,100,100);
}
else if(ExplodeFrame == 3)
{
ctxAnimation.drawImage(spriteSheet,200,2740,100,100,explodeX,explodeY,100,100);
}
else if(ExplodeFrame == 4)
{
ctxAnimation.drawImage(spriteSheet,300,2740,100,100,explodeX,explodeY,100,100);
}
else if(ExplodeFrame == 5)
{
ctxAnimation.drawImage(spriteSheet,400,2740,100,100,explodeX,explodeY,100,100);
}
else
{
ExplodeFrame = 0;
clearTimeout(ExplodeTimeout);
ctxAnimation.clearRect(0,0,canvasWidth,canvasHeight);
}
}
它应该以每秒5帧的速度显示动画,直到结束。这就是我想要做的。
答案 0 :(得分:1)
答案 1 :(得分:0)
setTimeout
运行您的代码一次。看来你正在寻找每200毫秒运行一次的东西。为此,请使用setInterval
。它的工作方式与您尝试使用setTimeout
的方式相同。当您使用setInterval
时,请通过调用clearInterval
答案 2 :(得分:0)
首先,用
替换if语句ctxAnimation.drawImage(spriteSheet,(ExplodeFrame - 1) * 100,2740,100,100,explodeX,explodeY,100,100);
其次,您只需调用DrawSpawnAnimation
方法一次。为了实际看到动画,你应该反复调用它。通过在DrawSpawnAnimation
:
function DrawSpawnAnimation()
{
...
if (ExplodeFrame < 5)
{
setTimeout(DrawSpawnAnimation, 20);
}
}