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

Collision Detection


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');

    //  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');
		glass = this.add.sprite(320, 310, '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)

function collisionHandler(obj1, obj2) {

    //  The two sprites are colliding
		// shot.kill();


function render() {



If anyone could help that'd be great! 

Kind regards!

Link to comment
Share on other sites

  • 3 years later...

  • Recently Browsing   0 members

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