Jump to content

Background random generation


quiphop
 Share

Recommended Posts

So I'v wrote that:
Generate function:

    spriteGenerator: function(){        this.backgroundObjects = this.game.add.group();        var randomSprite = null,            backgroundStep = 1200,            y = 70;        for(var i=0;i<4;i++){            randomSprite = this.game.rnd.integerInRange(1,4);            this.backgroundObjects.create();            var house = this.backgroundObjects.create(backgroundStep, y, 'house' + randomSprite);            house.checkWorldBounds = true;            house.outOfBoundsKill = true;            this.game.physics.enable(house);            house.body.velocity.x = globalGameSpeed;            backgroundStep-=500;        }            }

And update: 

    update: function() {        this.backgroundObjects.forEach(function(item) {            if(item.x + item.width < 0){                item.reset(1000,item.y);                item.loadTexture('house' + this.game.rnd.integerInRange(1,4));                item.body.velocity.x = globalGameSpeed;            }        }, this);     }

How do I optimize this code and improve perfomance?

Link to comment
Share on other sites

 Share

  • Recently Browsing   0 members

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