Jump to content

Collider not working properly with groups


manekin
 Share

Recommended Posts

Hello,

I'm trying to create a group of enemies, the problem is that collisions with tile map are not working. As you can see in the attached photo, enemies only collide with the bottom of a tile. Before I created a group, I was using a single sprite and everything was fine, also my main character collides without issues as well. 

Here's my code:

let map = this.add.tilemap('bcg', 32, 32, 1280, 720);
map.addTilesetImage('tileset');
let layer = map.createStaticLayer(0, 'tileset');
map.setCollision([0], true, layer);
this.enemies = this.add.group({
    classType: Enemy,
    active: false,
    maxSize: 5,
    runChildUpdate: true
);
this.physics.add.collider(this.enemies, layer);
class Enemy extends Phaser.GameObjects.Sprite {
    constructor(scene) {
        super(scene);
        scene.add.existing(this);
        scene.physics.world.enable(this);
        this.hp = 100;
        this.direction = 1;
        this.attackRadius = 300;

        this.ammo = null;
        this.fireRate = 500;
        this.lastFired = 0;
        this.firing = false;

        this.patrolDistance = 150;
        this.distTraveled = 0;

        this.body.collideWorldBounds = true;
    }

.
.
.

 

bug.jpg

Link to comment
Share on other sites

How do I create a physics group that contains Enemy objects? I'm assuming that you want me to use this.physics.add.group(), right? I'm sorry if this is a stupid question, but I'm completely new to phaser.

 

@Edit

I turned on physics debugging, it seems like the "hitbox" (is that what it's called?) is half the size it should be. 

bug3.jpg

 

@Edit2

I managed to make it work by adding this.body.height = 64; to the Enemy class' constructor. There must be a better solution, right?

Link to comment
Share on other sites

How are you setting the texture of the enemy sprites? If there's no texture specified when the body is created it will get the size of default one (32 × 32).

You need something like 

class Enemy extends Phaser.GameObjects.Sprite {
    constructor(scene, x, y, key, frame) {
        super(scene, x, y, key, frame);
        // …
    }
}

 

Link to comment
Share on other sites

How can I pass parameters to class' constructor while using group? Should I create an empty group and fill it with objects in a loop?

 

Would that work?

this.enemies = this.add.group();
for(int i = 0; i < 5; i++){
    this.enemies.add(new Enemy(this, 200, 200, 'enemy_sprite'));
}

 

Link to comment
Share on other sites

No, it didn't, perhaps i'm doing it wrong.

this.enemies = this.add.group({
    maxSize: 5,
    runChildUpdate: true
});

for (let i = 0; i < 5; i++) {
    this.enemies.add(new Enemy(this, Phaser.Math.Between(48, 1200), Phaser.Math.Between(0, 64), 'enemy_sprite', 0), true);
}

 

@Edit

Ok, it's working now, I forgot to pass the parameters in super().

Thank you for your help!

Link to comment
Share on other sites

 Share

  • Recently Browsing   0 members

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