我想隨機選擇一個語句,其中的值從裏面的精靈表中的某個點繪製。這是我現有的代碼。Javascript的html5帆布隨機開關語句
this.asteroid = Math.floor(Math.random()*4);
switch(this.asteroid)
{
case 1:
this.srcX = 0;
this.srcY = 528;
this.width = 32;
this.height = 33;
break;
case 2:
this.srcX = 32;
this.srcY = 528;
this.width = 32;
this.height = 33;
break;
case 3:
this.srcX = 64;
this.srcY = 528;
this.width = 32;
this.height = 33;
break;
case 4:
this.srcX = 0;
this.srcY = 565;
this.width = 62;
this.height = 60;
break;
}
我稍後繪製它選擇的值。
問題我有它的唯一繪製紫色/灰色小行星,目前情況2和3.我已經在主屏幕上設置了一種字體,告訴我哪種情況下它先繪製並刷新,直到它說1或4它仍然吸引2和3
一個問題是,Math.floor(的Math.random()* 4)將產生0-3的數字。如果你想讓它產生1-4的數字,在* 4) – Keeleon 2013-03-05 15:40:20
'Math.floor(Math.random()* 4)之後加+1;'可以返回0,1,2,3而不是4。你不明白爲什麼你看不到它返回1的情況。 – tafa 2013-03-05 15:40:37