Jump to content
This forum will be closing down. Please move to the respective dedicated project forums.

Collision Detection


barry_mul
 Share

Recommended Posts

Hi folks,

I'm new to this forum but I was wondering if someone could point me in the right direction or to a tutorial that'll help me out?

I can't seem to get the collision detection to work here. 

This is my code: 

    
 

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

function preload() {

    game.load.image('shot', 'assets/sprites/drop.png');
    game.load.image('spaceship', 'assets/sprites/spaceship.png');
    game.load.image('glass', 'assets/sprites/glass.png');
		game.scale.scaleMode = Phaser.ScaleManager.SHOW_ALL;
}

var spaceship;
var glass;
var shot;
var cursors;
var fireButton;

function create() {
		
    //  Creates 1 single bullet, using the 'shot' graphic
    shot = game.add.weapon(1, 'shot');
		game.physics.arcade.enable(shot);

    //  The shot will be automatically killed when it leaves the world bounds
    shot.bulletKillType = Phaser.Weapon.KILL_WORLD_BOUNDS;

    //  Because our shot is drawn facing up, we need to offset its rotation:
    shot.bulletAngleOffset = 90;

    //  The speed at which the shot is fired
    shot.bulletSpeed = -600;

    spaceship = this.add.sprite(320, 20, 'spaceship');
		game.physics.arcade.enable(spaceship);
	
		glass = this.add.sprite(320, 310, 'glass');
		game.physics.arcade.enable(glass);

			
    //  Tell the Weapon to track the 'player' Sprite, offset by 14px horizontally, 0 vertically
    shot.trackSprite(spaceship, 100, 180);

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

    fireButton = this.input.keyboard.addKey(Phaser.KeyCode.SPACEBAR);

}

function update() {
	
		// object1, object2, collideCallback, processCallback, callbackContext
		game.physics.arcade.overlap(shot, glass, collisionHandler, null, this);

    spaceship.body.velocity.x = 0;

    if (cursors.left.isDown)
    {
        spaceship.body.velocity.x = -300;
    }
    else if (cursors.right.isDown)
    {
        spaceship.body.velocity.x = 300;
    }

    if (fireButton.isDown)
    {
			shot.fire();
    }

}
	
function collisionHandler(obj1, obj2) {

    //  The two sprites are colliding
		// shot.kill();
	  shot.destroy();
		console.log("collision");

}
		

function render() {

    shot.debug();

}
	

If anyone could help that'd be great! 

Kind regards!

Link to comment
Share on other sites

  • 3 years later...
 Share

  • Recently Browsing   0 members

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