Jump to content

Random generation of sprites in a group


Recommended Posts

Hi all, I have a group where, I want 2 types of blocks to be generated from left to right or vice versa not really important, my randomization seems to work fine, but the problem is they only changes if I refresh the game, but if I run it as it is, it will always be the same kind of block until I refresh, any ideas?

createOnebloque: function(){

this.bloquecs = ["bloqueSprite","bloquelSprite"]; //////Here are the 2 sprites
this.bloquesr = this.bloquecs[Math.floor(Math.random()*2)]; ///Here I randomize but 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.bloques.enableBody = true;
    this.bloques.body.immovable = true;
    this.bloques.body.velocity.x = -2000;


This below goes on the create funtion:

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

Link to comment
Share on other sites

You create the random block first and then put it in a group. Items get killed in the group (not destroyed), meaning they will be reused later when you call firstExists. The randomness is set for the group once and then used over and over again, giving the same pattern. To fix this you have to randomize the sprites within the calling of an item in the group.

If you put both sprites in one spritesheet rather than different sprites you simply can call a random frame to randomize the blocks:

this.bloques.reset(1440, 1400);
this.bloques.frame = this.rnd.integerInRange(0,1);


Link to comment
Share on other sites

  • 2 months later...

  • Recently Browsing   0 members

    • No registered users viewing this page.
  • Create New...