我有一个组,我希望从左到右生成两种类型的块,反之亦然。这并不重要;我的随机化似乎工作正常,但问题是他们只有在我刷新游戏时才会改变。但是,如果我按原样运行它,它将永远是同一种块,直到我刷新。我该如何解决这个问题?
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);
答案 0 :(得分: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);
}
我没有对它进行过测试,但它应该有效。