gpt4 book ai didi

Javascript html5 canvas 随机 switch 语句图片

转载 作者:行者123 更新时间:2023-11-30 18:08:14 25 4
gpt4 key购买 nike

我正在尝试随机选择一个语句,其中包含从 sprite 表中的某个点绘制的值。这是我当前的代码。

    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 位置,它们在 Sprite 表中都是正确且匹配的。

下面是我用来绘制 Sprite 的代码:

this.drawX -= this.speed;
ctxEnemy.drawImage(imgSprite,
this.srcX+this.width,this.srcY,this.width,this.he‌​ight,
this.drawX,this.drawY,this.width,this.height);
this.checkEscaped();

最佳答案

Math.floor(Math.random()*7+1) 可以取 1 到 7 之间的值。删除 +1

关于Javascript html5 canvas 随机 switch 语句图片,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15275861/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com