quiphop Posted April 5, 2015 Share Posted April 5, 2015 I got a few different(width and height) sprites. I want to create dynamic background with random sprite generation and scrolling(my few sprites). Any ideas? Link to comment Share on other sites More sharing options...
quiphop Posted April 5, 2015 Author Share Posted April 5, 2015 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 More sharing options...
Recommended Posts