2016-05-14 8 views
0

私は、左から右へ、またはその逆の2種類のブロックを生成するグループがあります。それは本当に重要ではありません。私のランダム化はうまくいくようですが、ゲームをリフレッシュするだけで問題は変わります。しかし、それをそのまま実行すると、リフレッシュするまでは常に同じ種類のブロックになります。これをどうすれば解決できますか?グループ内のスプライトのフェーザランダム生成

createOnebloque: function() { 
    this.bloquecs = ["bloqueSprite", "bloquelSprite"]; ////// Here are the 2 sprites 
    this.bloquesr = this.bloquecs[Math.floor(Math.random() * 2)]; /// Here I randomize, but it only works when I refresh. 
    this.bloque = this.game.add.group(); 
    this.bloque.createMultiple(5, this.bloquesr); 
    this.bloque.setAll('anchor.x', 0.5); 
    this.bloque.setAll('anchor.y', 0.5); 
    this.bloque.setAll('outOfBoundsKill', true); 
    this.bloque.setAll('checkWorldBounds', true); 
}, 

makeBloques: function() { 
    this.bloques = this.bloque.getFirstExists(false); 
    if (this.bloques) { 
     this.bloques.reset(1440, 1400); 
     this.game.physics.arcade.enable(this.bloques); 
     this.bloques.enableBody = true; 
     this.bloques.body.immovable = true; 
     this.bloques.body.velocity.x = -2000; 
    } 
} 

これは、以下の作成目的球に行く:

this.game.physics.arcade.collide(this.bullets, this.bloque, this.collisionBulletBloque, null, this); 

答えて

1

あなたの問題はここから来ている:

this.bloquesr = this.bloquecs[Math.floor(Math.random() * 2)]; 

あなたは一度ランダムキーを選択して、上からそれを使用しています。

あなたは多分このような第一の機能書き換えることができます。

createOnebloque: function() { 
    this.bloquecs = ["bloqueSprite", "bloquelSprite"]; 
    this.bloque = this.game.add.group(); 

    for (var i = 0; i < 5; i++) { 
     this.bloque.create(this.bloquecs[Math.floor(Math.random() * this.bloquecs.length]); 
    } 
    this.bloque.setAll('anchor.x', 0.5); 
    this.bloque.setAll('anchor.y', 0.5); 
    this.bloque.setAll('outOfBoundsKill', true); 
    this.bloque.setAll('checkWorldBounds', true); 
} 

私はそれをテストしていませんが、それが動作するはずです。

+1

おかげさまで私はテストして元気に帰ってきました。 – Rafahc

+0

私は2つの要素(this.bloquecs)を1と2を繰り返し表示していますが、3つのモアを追加して配列内に5つの要素を作成すると、3つの要素がシャッフルされます。共通の行動? – Rafahc

関連する問題