Jump to content

OK why the heck doesn't the collision happen?


MackTuesday
 Share

Recommended Posts

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

function preload() {


    game.load.image('Stars', 'assets/Stars.png');
	game.load.spritesheet('enemy', 'assets/enemy.png');
    game.load.spritesheet('Demon', 'assets/The Blood Demon.png');
	game.load.spritesheet('bullet', 'assets/fireball.png');
	game.load.spritesheet('Rlaser', 'assets/red laser.png');
	game.load.spritesheet('boom', 'assets/explosion3.gif');
	game.load.audio('Falcon', 'assets/audio/laserSFX.mp3' );
	

}



var boom;
var enemy;
var Rlaser;
var sprite;
var weapon;
var cursors;
var fireButton;
var Demon;
var bullet;
var Falcon;
var back;
var back2;
var Stars;
var anim;
var loopText;
var counter = 0;
var keys;




function create() {
	//  Our bullet group
    bullets = game.add.group();
    bullets.physicsBodyType = Phaser.Physics.ARCADE;
    bullets.enableBody = true;
    bullets.createMultiple(30, 'bullet');
    bullets.setAll('anchor.x', 0.5);
    bullets.setAll('anchor.y', 1);
    bullets.setAll('outOfBoundsKill', true);
    bullets.setAll('checkWorldBounds', true);
	
    weapon = game.add.weapon(20, 'bullet');
    weapon.bulletKillType = Phaser.Weapon.KILL_WORLD_BOUNDS;
    weapon.bulletAngleOffset = 90;
    weapon.bulletSpeed = 400;

    sprite = this.add.sprite(320, 500, 'Demon');
    //game.physics.arcade.enable(sprite);	
	
	//  The enemies
    enemy = game.add.sprite(320, 100, 'enemy');
    enemy.enableBody = true;
    enemy.physicsBodyType = Phaser.Physics.ARCADE;
	
	
    //  Tell the Weapon to track the 'player' Sprite, offset by 14px horizontally, 0 vertically
    weapon.trackSprite(sprite, 14, 0);

    fireButton = this.input.keyboard.addKey(Phaser.KeyCode.SPACEBAR);
	
	// this is the code for the laser when firing
	Falcon = game.add.audio('Falcon');
}

function collisionHandler (bullet, enemy) {
    console.log("collision!");
    //  When a bullet hits an enemy we kill them both

}

function update() {

    //  Run collision
        console.log(game.physics.arcade.overlap(enemy, bullets, collisionHandler, null, this));
	// this link sthe firbutton and laser sound effect
    if (fireButton.isDown)  //error?
    {
        weapon.fire();
		Falcon.play();
    }
}

The space key makes the weapon fire. That works fine. And the when the weapon fires, I get a sound. Good.

But the bullet goes right through enemy. What's going on?

 

Link to comment
Share on other sites

hi @MackTuesday,

you don't need to create a 'bullets' group.

When you do

weapon = game.add.weapon(20, 'bullet');

you're creating a group of type Phaser.Weapon with 20 bullets.

 

So, if you want to perform a collision with 'enemy' and bullets, you do:

game.physics.arcade.overlap(enemy, weapon.bullets, collisionHandler, null, this)

 

cheers

Link to comment
Share on other sites

 Share

  • Recently Browsing   0 members

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