Jump to content

FPS drops when shooting bullets on device


paapie
 Share

Recommended Posts

Hi guys,

 

I'm quite new with Phaser.io and I love the support and documentation. A year ago I was developing my own engine but had some issues with rendering. Now I'm using Phaser it's pretty advanced but my javascript knowlegde is high :). Now I'm trying to make a simple top down 2d game. Just walk and shoot on an android device in the browser. When I just walk without shooting any bullets I got a FPS of 55-60 quite good :D. But when I shot a bullet and walk again my fps drops to 35-40 and sometimes lower.

How can I improve this?

 

Here is the code:

var game = new Phaser.Game(500,320, Phaser.AUTO, 'phaser-example', { preload: preload, create: create, update: update, render: render });function preload() {    game.stage.backgroundColor = '#007236';    game.load.image('bullet', 'assets/sprites/purple_ball.png');    game.load.image('player', 'assets/sprites/arrow.png');	game.time.advancedTiming = true;}var cursors;var d;var player;var playerX;var playerY;var playerSpeed = 300;var sprite;var bullets;var fireRate = 100;var nextFire = 0;function create() {    //  Modify the world and camera bounds    game.world.setBounds(-2000, -2000, 4000, 4000);	for (var i = 0; i < 100; i++)    {        game.add.sprite(game.world.randomX, game.world.randomY, 'mushroom');    }	game.physics.startSystem(Phaser.Physics.ARCADE);    game.add.text(420, 320, "- phaser -", { font: "32px Arial", fill: "#330088", align: "center" });   	bullets = game.add.group();    bullets.enableBody = true;    bullets.physicsBodyType = Phaser.Physics.ARCADE;			bullets.createMultiple(10, 'bullet');    bullets.setAll('checkWorldBounds', true);    bullets.setAll('outOfBoundsKill', true);		player = game.add.sprite(0, -1800, 'player');	player.anchor.setTo(0.5, 0.5);		game.physics.arcade.enable(player,Phaser.Physics.ARCADE);	game.camera.follow(player);	    cursors = game.input.keyboard.createCursorKeys();		//game.scale.scaleMode = Phaser.ScaleManager.SHOW_ALL;        //have the game centered horizontally    game.scale.pageAlignHorizontally = true;    //game.scale.pageAlignVertically = true;    //screen size will be set automatically	game.scale.setScreenSize(true);		GameController.init({		touchRadius :50,        left: {			position: {left: 100,bottom: 120},             type: 'joystick',            joystick: {				radius :30,                touchStart: function() {                    // Don't need this, but the event is here if you want it.                },                touchMove: function(joystick_details) {                    game.input.joystickLeft = joystick_details;                },                touchEnd: function() {                    game.input.joystickLeft = null;                }            }        },        right: {			position: {				right: '5%'			},			type: 'buttons',			buttons: [			{				label: 'Shoot', fontSize: 13, touchStart: function() {					// do something					fire();				}			},			false, false, false			]		}    });}function update() {    if (cursors.up.isDown){		player.y -=4;    }    else if (cursors.down.isDown){		player.y += 4;    }    if (cursors.left.isDown){		player.x -= 4;    }    else if (cursors.right.isDown){		player.x += 4;    }	if (game.input.joystickLeft) {        // Move the ufo using the joystick's normalizedX and Y values,        // which range from -1 to 1.		player.rotation = Math.atan2(game.input.joystickLeft.normalizedY,  game.input.joystickLeft.normalizedX)*-1;		player.body.velocity.setTo(game.input.joystickLeft.normalizedX * 200, game.input.joystickLeft.normalizedY * playerSpeed * -1);	}    else {		player.body.velocity.setTo(0, 0);    }}function fire(input) {    if (game.time.now > nextFire && bullets.countDead() > 0)    {        nextFire = game.time.now + fireRate;        var bullet = bullets.getFirstDead();        bullet.reset(player.x - 8, player.y - 8);		game.physics.arcade.velocityFromRotation(player.rotation, 1000, bullet.body.velocity);    }}function render() {	game.debug.text('Active Bullets: ' + game.time.fps + ' / ' + bullets.length, 32, 32);    //game.debug.cameraInfo(game.camera, 32, 32);}

Later on I will use prototype etc. hope you guys can help me.

 

Greetings,

 

Frank

Link to comment
Share on other sites

Without looking too deeply - throw some debug stuff in there, i.e. how many bullets is it actually firing at once? and are they being pooled correctly? Also swap "game.time.now" for "game.time.time" or one of the physicsElapsed values, as "now" can often contain high precision timer values (depending on the browser).

Link to comment
Share on other sites

Thanks administrator for the quick reaction! I just changed my fire() function to:

function fire(input) {    if (game.time.time > nextFire && bullets.countDead() > 0)    {        nextFire = game.time.time + fireRate;		bullet = bullets.getFirstExists(false);		if(bullet){			bullet.reset(player.x - 8, player.y - 8);			bullet.lifespan = 500;			game.physics.arcade.velocityFromRotation(player.rotation, 1000, bullet.body.velocity);		}    }}

It works better now, the lifespan was to high :)

 

Greetz Frank

Link to comment
Share on other sites

 Share

  • Recently Browsing   0 members

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