Jump to content

Collision not working (Group of objects + Tileset)


CarlosBR
 Share

Recommended Posts

Collision works with single Sprite and the Map, but it doesn't with objects created with function createFromObjects.

The callback function never displays the console.log() message. The sprites move through the tiles, never colliding with the map.

I'm new to phaser3, so maybe I'm missing something. Here's the code:

preload() {       
        this.load.image('tiles', 'assets/levels_tileset.png');
        this.load.tilemapTiledJSON('level1', 'assets/maps/map1.json');        
        this.load.spritesheet('player', 'assets/dude.png', {frameWidth: 32, frameHeight: 48});
        this.load.spritesheet('enemies', 'assets/spritesheet_enemies.png', {frameWidth: 47, frameHeight: 47});        
}
    
create() {        
        var map = this.make.tilemap({ key: 'level1' });        
        var tiles = map.addTilesetImage('levels_tileset', 'tiles');        
        this.layer = map.createDynamicLayer('blocks', tiles, 0, 0);
        this.layer.setCollisionByExclusion([-1]);        
        this.player = this.physics.add.sprite(100, 450, 'player');
        this.player.setBounce(0.2);
        this.player.setCollideWorldBounds(true);
        this.player.body.setGravityY(300);        
        this.physics.add.collider(this.layer, this.player);        
        this.right = true;
        this.enemies = this.physics.add.group();
        this.enemies = map.createFromObjects('enemies_objects', 'enemy1', { key: 'enemies' });
        this.enemies.forEach((child) => {
            if(child instanceof Phaser.GameObjects.Sprite) {
                child.setFrame(4);
            }
        });
        this.physics.add.collider(this.enemies, this.layer, this.bounce, null, this);      
}
    
update() {
        this.enemies.forEach((enemy) => {
            if(enemy instanceof Phaser.GameObjects.Sprite) {
                if(this.right === true) {
                    enemy.x += 1;
                }
                else {
                    enemy.x -= 1;
                }
            }
        });
}
    
bounce() {
        console.log("Collision!");
        if(this.right === true) {
            this.right = false;
        }
        else {
            this.right = true;
        }
}

How can I fix this?

Link to comment
Share on other sites

Quote

this.physics.world.enable(this.enemies);

It still doesn't work. Is there any way I can use map.createFromObjects() to create sprites that already exists (hardcoded or in a class) instead of a tileset from TileMap? That way I could always create sprites with map.createFromObjects(), no matter if it's a single sprite, or thousands.

Link to comment
Share on other sites

I tried the same on http://labs.phaser.io/view.html?src=src/game objects\tilemap\static\create from objects.js and it worked, so I'm not sure.

Turn on physics debugging and debug-draw the tilemap layer (see the examples). That will show you if the sprites have physics bodies and if the tile collision edges are correct.

Technically I think you should be doing 

this.enemies = this.physics.add.group();
this.enemies.addMultiple(
  map.createFromObjects('enemies_objects', 'enemy1', { key: 'enemies' })
);

instead of overwriting this.enemies, but I'm not sure that will make a difference.

Link to comment
Share on other sites

The code:

this.enemies.addMultiple( map.createFromObjects('enemies_objects', 'enemy1', { key: 'enemies' }) );

results in this message:  this.enemies.forEach is not a function

When I active debug, I see retangles around the moving enemies, but they continue not colliding with tileset.

Link to comment
Share on other sites

 Share

  • Recently Browsing   0 members

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