ranom Posted June 27, 2014 Share Posted June 27, 2014 Hello! i'm creating a bullet-hell type game in phaser. When alot of bullet sprites are being created ingame (~10 sprites/second, size = 5x5 px), i get consistent lagspikes, and graphic bugs caused by the lag... Is this normal? Anything i can do to increase performance? Here's the code to create bullet sprites:...this.bulletGroup = game.add.group();this.bulletGroup.enableBody = true;this.bulletGroup.physicsBodyType = Phaser.Physics.ARCADE;this.bulletList = []...this.timeCheck = game.time.now; this.shoot = function(bulletSpeed, bulletRate, bulletTexture){ if (game.time.now > this.timeCheck + bulletRate) { var bullet = this.bulletGroup.create(this.sprite.x, this.sprite.y, bulletTexture); bullet.anchor.setTo(0.5,0.5); game.physics.arcade.moveToXY(bullet, player.sprite.x, player.sprite.y, bulletSpeed); this.bulletList.push(bullet); this.timeCheck = game.time.now; }}; this.shoot_update = function(type, phase){ for(var i = 0; i < this.bulletList.length; i++){ this.bulletList[i].body.x += Math.sin(phase*0.1)*5; }};// this.shoot and this.shoot_update are called in update() Link to comment Share on other sites More sharing options...
ranom Posted June 27, 2014 Author Share Posted June 27, 2014 I managed to fix the lag spikes by creating invisible sprites before updating. I grab an invisible sprite each 0.1 second, set it's alpha to 1 and update the coordinates.Is there a better way? Link to comment Share on other sites More sharing options...
morosenerd Posted June 27, 2014 Share Posted June 27, 2014 I lack the specific JS/Phaser-fu to give you exact code, but generally you should be pooling your bullets (and other resources in fact). Basically, when you create a group (in the state's create function), immediately add X bullets and temporarily hide them by using the kill() function. Then, when you need a bullet, find the first "killed" (inactive) and use revive(). I did it a lot in AS3/Haxe, and the performance gain there was huge, I suspect it should also help here. Link to comment Share on other sites More sharing options...
SeanEBaby Posted June 27, 2014 Share Posted June 27, 2014 See this example for how to do what morosenerd is describing. Link to comment Share on other sites More sharing options...
lewster32 Posted June 27, 2014 Share Posted June 27, 2014 Yeah, this example also shows it applied to bullets: http://gamemechanicexplorer.com/#bullets-2 A word of caution: setting a sprite's alpha to 0 has no effect on performance - as far as the renderer is concerned it's still drawing the sprite, you just can't see it. If you need to stop a sprite being rendered entirely, use sprite.visible = false. clark 1 Link to comment Share on other sites More sharing options...
ranom Posted June 27, 2014 Author Share Posted June 27, 2014 thanks! i'll check that out. Link to comment Share on other sites More sharing options...
Claudiovc Posted June 30, 2014 Share Posted June 30, 2014 By the way, maybe you could extract the Math.sin() from the loop...I don't know the size of you bulletList array, but, since it doesn't depend on the loop variable...this.shoot_update = function(type, phase){ var dx = Math.sin(phase*0.1)*5; for(var i = 0; i < this.bulletList.length; i++){ this.bulletList[i].body.x += dx; }}Maybe it'll be just a little improvement....but the topic is about the performance, so why not =)I think you could use bulletGroup.forEach() for the loop, as well. Link to comment Share on other sites More sharing options...
Recommended Posts