Weird delay or bug on weapon and group collision

Recommended Posts

Hello Everyone!

I´m testing and choosing a javascript engine to create a game, and I found something weird in Phaser which I can´t fix it.

I made a little game just to test how the framework works. I´ve created a sprite, a weapon and a group of enemy. On the collision event between weapon and enemy group the behavior seems weird or bug. The collision event is called, but seems a delay, sometimes work, sometimes no. I don´t know what is happening.

Can someone help and tell what is wrong on the code?

Here is a link with game running:

Sorry for identation on snippet, here on my editor seems all idents aligned.

Sorry for var names in portuguese too :)


var game = new Phaser.Game(800, 600, Phaser.CANVAS, 'phaser-example', { preload: preload, create: create, update: update, render: render });

function preload() {
	//game.load.crossOrigin = 'anonymous';
	//  37x45 is the size of each frame
    //  There are 18 frames in the PNG - you can leave this value blank if the frames fill up the entire PNG, but in this case there are some
    //  blank frames at the end, so we tell the loader how many to load
    game.load.spritesheet('president', 'img/temer2.png', 340, 500, 6);	
	game.load.spritesheet('petista', 'img/petista.png', 272, 460, 2);
	game.load.image('bullet', 'img/bullet.png');
	game.load.image('background', 'img/congresso2.jpg'); 
	//  Firefox doesn't support mp3 files, so use ogg'shoot', ['sounds/shoot.mp3']);

var temer;
var weapon;
var shoot_sound;
var background;
var petistas;

function create() 
	cursors = this.input.keyboard.createCursorKeys();

    fireButton = this.input.keyboard.addKey(Phaser.KeyCode.SPACEBAR);
	shoot_sound ='shoot');    
	background = game.add.image(0, 0, 'background');
	petistas =; // create group
	petistas.enableBody = true;
    petistas.physicsBodyType = Phaser.Physics.ARCADE;      
	petistas.setAll('outOfBoundsKill', true);
    petistas.setAll('checkWorldBounds', true);
	petistas.setAll('anchor.x', 0.5);
    petistas.setAll('anchor.y', 0.5);
	//  center the picture in the world
    background.alignIn(, Phaser.CENTER);

function update() 
	if (game.input.keyboard.isDown(Phaser.Keyboard.LEFT))
        temer.x -= 4;'walk', 6, true);		
		if(temer.scale.x == 0.3)
			temer.scale.x = -0.3;
			weapon.trackSprite(temer, -60, -18);
			weapon.fireAngle = 180;
    else if (game.input.keyboard.isDown(Phaser.Keyboard.RIGHT))
        temer.x += 4;'walk', 6, true);		
		if(temer.scale.x == -0.3)
			temer.scale.x = 0.3;
			weapon.trackSprite(temer, 60, -18);  
			weapon.fireAngle = 0;			
	else if (game.input.keyboard.isDown(Phaser.Keyboard.UP))
        temer.y -= 4;'walk', 6, true);
    else if (game.input.keyboard.isDown(Phaser.Keyboard.DOWN))
        temer.y += 4;'walk', 6, true);
		temer.animations.stop('walk', true);
	if (fireButton.isDown)

function render() {
    //game.debug.spriteInfo(temer, 20, 32);
	/*for (var i = 0; i < petistas.length; i++)

function initTemer()
	temer = game.add.sprite(,, 'president');
    temer.anchor.setTo(0.5, 0.5);
	temer.scale.setTo(0.3, 0.3);

function initWeapon()
	//  Creates 1 single bullet, using the 'bullet' graphic
    weapon = game.add.weapon(30, 'bullet');
    //  The bullet will be automatically killed when it leaves the world bounds
    weapon.bulletKillType = Phaser.Weapon.KILL_WORLD_BOUNDS;  

    //  The speed at which the bullet is fired
    weapon.bulletSpeed = 400;   
	//  Speed-up the rate of fire, allowing them to shoot 1 bullet every 60ms
	weapon.fireRate = 60;
	weapon.bulletAngleOffset = 0;
	weapon.fireAngle = 0;

    //  Tell the Weapon to track the 'player' Sprite, offset by 14px horizontally, 0 vertically
    weapon.trackSprite(temer, 60, -18);   

function createPetistas() 
	var petista_height = 184;
	var y_point =;
	if(game.height - petista_height > y_point)
		y_point = game.height - petista_height;
	var petista = petistas.create(0, y_point, 'petista');
    petista.scale.setTo(0.4, 0.4);   
	petista.animations.add('walk');'walk', 2, true);
	game.add.tween(petista).to({ x: game.width + (1600 + petista.x) }, 20000, Phaser.Easing.Linear.None, true);
	game.physics.enable(petista, Phaser.Physics.ARCADE);
	// add colision event
	game.physics.arcade.overlap(weapon.bullets, petistas, collisionHandlerPetista, null, this);
	var MAX_ENEMY_SPACING = 3000;, MAX_ENEMY_SPACING), createPetistas);

function collisionHandlerPetista(bullet, petista)
	console.log('colision detected');


Share this post

Link to post
Share on other sites

If it was an event, Phaser would have to test all game objects for all possible collisions all the time.

Instead, when you call overlap, you're causing the physics engine to check for overlaps, and if it returns positively your callback is invoked.

Share this post

Link to post
Share on other sites

I understand hexus.

But the update function isn't called on miliseconds interval? I guess calling a overlap function on every x miliseconds, has same effect of check all objects, to find any collisions every x miliseconds.

On desktop the colisions worked like expected, but I tested the game on a mobile with quadcore processor and 4GB memory ram, and the game was slow, with lags. Seems device can't process so many sprites and collisions event at the same time.

It's a problem to me, since I want the game work on mobile devices to.

Share this post

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

  • Recently Browsing   0 members

    No registered users viewing this page.