我正在尝试随机选择其中包含值的语句,该语句从精灵表中的某个点绘制。这是我拥有的当前代码。
this.asteroid = Math.floor(Math.random()*7+1);
switch(this.asteroid)
{
case 0:
this.srcX = 0;
this.srcY = 528;
this.width = 32;
this.height = 33;
break;
case 1:
this.srcX = 32;
this.srcY = 528;
this.width = 32;
this.height = 33;
break;
case 2:
this.srcX = 64;
this.srcY = 528;
this.width = 32;
this.height = 33;
break;
case 3:
this.srcX = 63;
this.srcY = 565;
this.width = 62;
this.height = 60;
break;
case 4:
this.srcX = 125;
this.srcY = 565;
this.width = 62;
this.height = 60;
break;
case 5:
this.srcX = 187;
this.srcY = 565;
this.width = 62;
this.height = 60;
break;
case 6:
this.srcX = 0;
this.srcY = 632;
this.width = 116;
this.height = 120;
break;
}
然后我稍后会绘制它选择的值。
我的问题主要是绘制所有这些,但同时只绘制一个空白图像,我检查了所有 X 和 Y 位置,它们都是正确的并且在精灵表中匹配。
下面是我用来绘制精灵的代码:
this.drawX -= this.speed;
ctxEnemy.drawImage(imgSprite,
this.srcX+this.width,this.srcY,this.width,this.height,
this.drawX,this.drawY,this.width,this.height);
this.checkEscaped();