saibotlive Posted October 30, 2014 Share Posted October 30, 2014 I have a side scrolling game I am working on where vehicles coming from the right and are destroyed once off screen on the left. To do this I created a timed event for how often they should come in and add a random vehicle. I create a sprite every time the event is triggered. I was wondering if there's a better way of doing this? (Eg by reusing sprites but also making sure the vehicles don't overlap each other at anytime) It runs fine on my browser and mobile but I just want the best performance. Are there also drawbacks of using timed events to add sprites? Thanks in advance. This is an example of how the vehicles are added below addVehicle:function(){ //console.log('add'); this.game.time.events.remove(this.vehEvent); this.vehicles.push(new Vehicles(this.game, this.player, this.posArray, this.prevW)); this.prevW = Number(this.vehicles[this.vehicles.length - 1].width)*2; this.vehEvent = this.game.time.events.add(Phaser.Timer.SECOND * game.rnd.realInRange(1,1.2), this.addVehicle, this); } Link to comment Share on other sites More sharing options...
Sam Posted October 30, 2014 Share Posted October 30, 2014 Check if a vehicle is out of bounds:this.vehicle.events.onOutOfBounds.add(this.resetVehicle, this);You could reset the x position of the vehicle.And also add another sprite etc if you want.this.sprite.reset(this.sprite.x - game.width); Link to comment Share on other sites More sharing options...
saibotlive Posted October 30, 2014 Author Share Posted October 30, 2014 Yeah I destroy it when it's out of bounds but with no reset. The vehicles about 5 in total, all have different properties and animations and I was wondering by just resetting that vehicle would make it look less random, where I want a more random experience. Is it ok adding a new sprite or is it preferable to just add a pool of sprites in an array from the beginning,which come in as random as possible?. Link to comment Share on other sites More sharing options...
Sam Posted October 30, 2014 Share Posted October 30, 2014 You can add a new Sprite when you are resetting the sprite -> Add a new Sprite randomly.I do not know if this is good or bad for performance.What is more performant is to reuse the vehicles. Instead of creating and destroying them, just reset them.- my opinion and experience, if I'm incorrect- please someone correct me.- Link to comment Share on other sites More sharing options...
valueerror Posted October 30, 2014 Share Posted October 30, 2014 definitely reset the position of the sprite randomly instead of creating new ones all the time.. otherwise you are just feeding the garbage collecter which will lead to lags.. create a pool and reuse sprites.. you may reconfigure them on reset to make it more random... dont use outofboundskill if thats possible because checkoutofbounds all the time is not good for performance Sam 1 Link to comment Share on other sites More sharing options...
saibotlive Posted October 30, 2014 Author Share Posted October 30, 2014 Thanks a lot guys, I plan to rewrite my code to use pools and reset the sprites. Is it also ok to stick with the timed events? Link to comment Share on other sites More sharing options...
Recommended Posts