barry_mul Posted January 24, 2017 Share Posted January 24, 2017 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 More sharing options...
samme Posted January 24, 2017 Share Posted January 24, 2017 You need to test the Bullets group itself, not the Weapon. game.physics.arcade.overlap(glass, shot.bullets, collisionHandler, null, this); // … function collisionHandler(obj, bullet){ bullet.kill(); // … } barry_mul 1 Link to comment Share on other sites More sharing options...
barry_mul Posted January 24, 2017 Author Share Posted January 24, 2017 Thanks a million! I should have spotted that sooner. Link to comment Share on other sites More sharing options...
Arpan Taneja Posted April 30, 2020 Share Posted April 30, 2020 (edited) . Edited May 14, 2020 by Arpan Taneja . Link to comment Share on other sites More sharing options...
Recommended Posts