1

スプライト シートの特定のポイントから描画される値を含むステートメントをランダムに選択しようとしています。これは私が持っている現在のコードです。

    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。

4

2 に答える 2

2

あなたのコードは現在、0、1、2、および 3 の数字を生成していcase 1:ますcase 4:。これを修正するには、ケースを調整するだけです。

個人的には、このコードを次のように書き直します。

var srcX = [0,32,64,0],
    srcY = [528,528,32,565],
    width = [32,32,32,62],
    height = [33,33,33,60],
    rand = Math.floor(Math.random()*4);
this.srcX = srcX[rand];
this.srcY = srcY[rand];
this.width = width[rand];
this.height = height[rand];
于 2013-03-05T15:41:22.113 に答える
0
this.asteroid = Math.floor(Math.random()*4);

this.asteroid = Math.floor(Math.random()*5)+1;//である必要があり ますcase 4case 0

コンソールで参照してください: http://jsbin.com/ulirom/3/

于 2013-03-05T15:40:08.707 に答える