manekin Posted November 27, 2018 Share Posted November 27, 2018 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; } . . . Link to comment Share on other sites More sharing options...
manekin Posted November 27, 2018 Author Share Posted November 27, 2018 Also watch what happens when i turn off tile map's collisions. It's like phaser thinks the sprite ends in the middle of the image. Link to comment Share on other sites More sharing options...
samme Posted November 27, 2018 Share Posted November 27, 2018 You may have to use a physics group for that. Also turn on physics debugging, just in case. Link to comment Share on other sites More sharing options...
manekin Posted November 27, 2018 Author Share Posted November 27, 2018 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. @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 More sharing options...
samme Posted November 27, 2018 Share Posted November 27, 2018 Are you doing anything else to size/scale the enemy sprites or their textures? Because ordinarily the hitboxes should be the "right" size without you having to do anything special. Link to comment Share on other sites More sharing options...
manekin Posted November 27, 2018 Author Share Posted November 27, 2018 No, nothing. This happens only with groups, single sprites are the "right" size. Link to comment Share on other sites More sharing options...
samme Posted November 28, 2018 Share Posted November 28, 2018 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 More sharing options...
manekin Posted November 28, 2018 Author Share Posted November 28, 2018 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 More sharing options...
samme Posted November 28, 2018 Share Posted November 28, 2018 That should work. If you don't need extra params than you can just use this.enemies.create(200, 200, 'enemy_sprite'); Did it fix the hitboxes? Link to comment Share on other sites More sharing options...
manekin Posted November 28, 2018 Author Share Posted November 28, 2018 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 More sharing options...
manekin Posted November 28, 2018 Author Share Posted November 28, 2018 I've got another question though. I created a dying animation, the last two frames are different height and the sprite "floats". Is there a way to make the hitbox match frame size? Link to comment Share on other sites More sharing options...
Recommended Posts