Jump to content

Collider not working between Bullets group and enemy sprite


squeege
 Share

Recommended Posts

Hello,

I am having an issue getting colliders OR Overlaps to work between my bullets physics group and my enemies physics sprite. I can get the collision to work between my ship and enemy, im not sure what i am doing wrong with the bullets group though. Below is the entirety of my code. You can see some that i edited out but wanted to keep the code. Any help would be appreciated. The bullets go right over the enemy and do not register a collision or an overlap.

 

 

<!DOCTYPE html>
<html>
<head>
<script src="C:\Phaser\phaser-3.15.1\phaser-3.15.1\dist/phaser-arcade-physics.min.js"></script>
</head>
<body>
 
<script>




 
var config = {
type: Phaser.AUTO,
width: 800,
height: 600,
physics: {
default: 'arcade',
arcade: {
debug: true,
gravity: { y: 0 }
}
},
scene: {
preload: preload,
create: create,
update: update,
extend: {
checkBulletVsEnemy: checkBulletVsEnemy,
// launchEnemy: launchEnemy,
// hitShip: hitShip,
// hitEnemy: hitEnemy
}
}
// update: update
 
};
var Bullet = new Phaser.Class({
Extends: Phaser.Physics.Arcade.Image,
initialize:
function Bullet (scene)
{
Phaser.Physics.Arcade.Image.call(this, scene, 0, 0, 'bullet');
this.physics = true;
this.speed = Phaser.Math.GetSpeed(400, 1);
 
},
 
fire: function (x, y)
{
this.setPosition(x+50, y);
this.setActive(true);
this.setVisible(true);
},
 
update: function (time, delta)
{
this.x += this.speed * delta;
if (this.x > 850)
{
this.setActive(false);
this.setVisible(false);
}
}
 
});
var bullets;
var speed;
var game = new Phaser.Game(config);
var enemy;
var ship;
 
function preload ()
{
this.load.image("background", "assets/stars.png");
this.load.image("ship", "assets/ships/f1.png");
this.load.image("bullet", "assets/ships/bullet.png")
this.load.spritesheet('ships', 'assets/ships/Spritesheet_64x29.png', {frameWidth: 64, frameHeight: 29});
this.load.spritesheet('enemies', 'assets/ships/eSpritesheet_40x30.png', {frameWidth: 40, frameHeight: 30});
};
 
function create ()
{
 
bullets = this.physics.add.group({
classType: Bullet,
maxSize: 10,
runChildUpdate: true
});
keySpace = this.input.keyboard.addKey(Phaser.Input.Keyboard.KeyCodes.SPACE);
cursors = this.input.keyboard.createCursorKeys();
this.anims.create({
key: 'run',
frames: 'ships',
frameRate: 20,
repeat: -1
});
this.anims.create({
key: 'run',
frames: 'enemies',
frameRate: 20,
repeat: -1
});
this.tileSprite = this.add.tileSprite(400,300,800,600, 'background')
ship = this.physics.add.sprite(400,300,'ships').play('run');
ship.setCollideWorldBounds(true);
enemy = this.physics.add.sprite(500,200, 'enemies');
this.physics.add.collider(ship, enemy);
 
// this.physics.add.collider(bullets, enemy, this.hitEnemy, this.checkBulletVsEnemy, this);
// this.physics.add.overlap(bullets, enemy);
 
};
 
 
 
function update (time,delta)
{
this.tileSprite.tilePositionX += 0.5; //change this to a value suited for your needs change - to + to change direction
enemy.body.debugBodyColor = enemy.body.touching.none ? 0x0099ff : 0xff9900;
// this.physics.add.collider(Bullet, enemy);
this.physics.add.collider(bullets, enemy);
if (cursors.left.isDown)
{
ship.setVelocityX(-160);
}
else if (cursors.right.isDown)
{
ship.setVelocityX(160);
}
else if (cursors.down.isDown)
{
ship.setVelocityY(160);
}
else if (cursors.up.isDown)
{
ship.setVelocityY(-160);
}
else
{
ship.setVelocityX(0);
ship.setVelocityY(0);
}
if (keySpace.isDown)
{
var bullet = bullets.get();
if (bullet)
{
bullet.fire(ship.x, ship.y);
}
}
};
function checkBulletVsEnemy ()
{
// return (bullet.active && enemy.active);
console.log("Hit enemy");
}
 
</script>
 
</body>
</html>
Link to comment
Share on other sites

  • 3 weeks later...

I was just reading the code and I didn't compile the code, so someone else may answer this better...

Make sure you're initiating the physics body on the Bullet class, usually I add two lines under Phaser.Physics.Arcade.Image.call(this, scene, 0, 0, 'bullet');   

    this.scene.physics.world.enable(this);
    this.scene.add.existing(this);

There's a tutorial in this newsletter that better explains all this: https://madmimi.com/p/eeb5ec

Link to comment
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...
 Share

  • Recently Browsing   0 members

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