Jump to content

No collisions between bullets and tilemap


kurhlaa
 Share

Recommended Posts

Hello!

According to the examples I've created a tilemap from JSON file and added firing bullets with a left mouse clicking in a cursor's direction. Also a platform was added. My problem is that there are no collisions detected between a bullet and a tilemap's objects. But collisions works fine between the bullet and a platform.

Here is my demo code:

var config = {
    type: Phaser.CANVAS,
    width: 800,
    height: 600,
    backgroundColor: '#2d2d2d',
    parent: 'phaser-example',
    physics: {
        default: 'arcade',
        arcade: {
            gravity: { y: 300 },
            debug: false
        }
    },
    scene: {
        preload: preload,
        create: create
    }
};

var game = new Phaser.Game(config);
var bullets;
var platforms;

function preload ()
{
    this.load.tilemapTiledJSON('map', 'assets/tilemaps/maps/impact-tilemap.json');
    this.load.image('kenney', 'assets/tilemaps/tiles/kenney.png');
    this.load.image('ground', 'src/games/firstgame/assets/platform.png');
    this.load.image('bullet', 'src/games/firstgame/assets/star.png');
}

function create ()
{
    var map = this.make.tilemap({ key: 'map' });
    var tileset = map.addTilesetImage('kenney');
    var layer = map.createStaticLayer(0, tileset, 0, 0);
    layer.setCollisionByExclusion([-1]);
    
    this.physics.world.bounds.width =  layer.width;
    this.physics.world.bounds.height = layer.height;

    platforms = this.physics.add.staticGroup();
    platforms.create(400, 268, 'ground');

    // Fires bullet on left click of mouse
    this.input.on('pointerdown', function() {
		fire(this);
    }, this);


    var Bullet = new Phaser.Class({

        Extends: Phaser.GameObjects.Image,

        initialize: function Bullet(scene)
        {
            Phaser.GameObjects.Image.call(this, scene, 0, 0, 'bullet');

            this.speed = Phaser.Math.GetSpeed(300, 1);
			this.velocity = new Phaser.Geom.Point(0, 0);
        },

        fire: function (x, y, direction)
        {
            this.setPosition(x, y);

            this.setActive(true);
            this.setVisible(true);

			this.velocity.setTo(0, -this.speed);
            Phaser.Math.Rotate(this.velocity, direction);
        },

        update: function (time, delta)
        {
            // Update position based on velocity
            this.x += this.velocity.x * delta;
            this.y += this.velocity.y * delta;
        }

    });

    bullets = this.physics.add.group({
        classType: Bullet,
        maxSize: 30,
        runChildUpdate: true
    });


    this.physics.add.collider(bullets, platforms, callbackFunc, null, this);
    this.physics.add.collider(bullets, layer,     callbackFunc, null, this);
}

function callbackFunc(bullet, target)
{
    if ( bullet.active === true ) {
        console.log("Hit!");

        bullet.setActive(false);
        bullet.setVisible(false);
    }
}

function fire(that)
{
    var bullet = bullets.get();

    if (bullet) {
        bullet.body.allowGravity = false;

        var angle = Math.atan2(that.input.activePointer.y - 400, that.input.activePointer.x - 300);

        bullet.fire(300, 400, angle + (3.14/2));
    }
}

You can copy-paste it to any example at https://labs.phaser.io and start clicking to fire at any direction.

I've tried to add a standard player from examples too and collisions work fine with the tilemap's objects. So the problem is with bullets only.

 

Please help to solve this. Thanks!

Link to comment
Share on other sites

 Share

  • Recently Browsing   0 members

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