Phaser 3,项目符号与图块图之间无冲突

时间:2018-06-22 17:44:40

标签: javascript collision-detection phaser-framework

根据Phaser 3示例,我已经从JSON文件创建了一个tilemap,并用鼠标左键单击光标方向添加了发射子弹。还增加了一个平台。我的问题是,在项目符号和tilemap的对象之间没有检测到碰撞。但是子弹和平台之间的碰撞可以正常工作。

这是我的演示代码:

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));
    }
}

您可以将其复制粘贴到https://labs.phaser.io上的任何示例中,然后开始单击以向任意方向射击。

我也尝试从示例中添加一个标准播放器,并且与tilemap的对象发生冲突时效果很好。所以问题只在于子弹。

请帮助解决此问题。谢谢!

0 个答案:

没有答案
相关问题