Sign in to follow this  
ranom

memory management/performance/bottlenecks

Recommended Posts

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()

Share this post


Link to post
Share on other sites

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?

Share this post


Link to post
Share on other sites

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.

Share this post


Link to post
Share on other sites

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.

Share this post


Link to post
Share on other sites

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.

Share this post


Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
Sign in to follow this  

  • Recently Browsing   0 members

    No registered users viewing this page.