Sign in to follow this  
barry_mul

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

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.