尝试检测相位器 3 中是否有 2 个特定物体与 matter.js 发生碰撞

Trying to detect if 2 specific bodies are colliding with matter.js in phaser 3

我正在尝试使用物质物理引擎将敌人添加到我的平台游戏中,但是使用 this.matter.world.on collisionactive 函数仅在玩家跳跃一次后检查地板和敌人之间的碰撞。我目前正在使用标签来检查碰撞。我曾尝试添加额外的条件,但只能让玩家无限跳跃。即-它正在检查碰撞的标签。

碰撞检测代码:

    this.matter.world.on("collisionactive", (e,o1,o2) => {
        if(o1.label == 'floor' && o2.label == 'player')
        {
            this.touchingGround = true;
            console.log('touching')
        }
    });

敌人创建函数: 现在敌人是当玩家按下 f

时在光标处创建的立方体
function createEnemy(scene,x,y)
{
    enemy = scene.matter.add.image(x,y,'enemy').setScale(1.5)
    enemy.body.label = 'enemy'
}

我不完全理解这个问题,但是如果你对 checking/finding 所有当前的碰撞有疑问,你可以使用 pairs 属性 的 event-object-argument (link to the documentation),传递给 physics-callback函数。此 属性 应包含其他碰撞。

"...The event.pairs array may contain more colliding bodies. ..."

这里有一个简短的演示:

document.body.style = 'margin:0;';
var config = {
    type: Phaser.AUTO,
    width: 11 * 16, 
    height: 6 * 16,
    zoom: 2,
    pixelArt: true,
    scene: {
        preload: preload,
        create: create
    },
    physics: {
        default: 'matter',
        matter: {
            gravity: {
                y:.3
            },
            debug:true
        }
    },
    banner: false
};

function preload (){
    this.load.image('mario-tiles', 'https://labs.phaser.io/assets/tilemaps/tiles/super-mario.png');
}

function create (){
    var level = [
      '0'.repeat(11).split(''),
      '0'.repeat(11).split(''),
      '0'.repeat(11).split(''),
      '0'.repeat(11).split(''),
      '0'.repeat(11).split(''),
      '0'.repeat(11).split('').map( _ => 14),
    ];        
   
    var map = this.make.tilemap({ data: level, tileWidth: 16, tileHeight: 16 });
    var tiles = map.addTilesetImage('mario-tiles');
    var layer = map.createLayer(0, tiles, 0, 0);

    this.add.text(4, 4, 'click to jump', {color:'#9EE6FF', fontSize:10})
        .setOrigin(0);
    var info = this.add.text(4, 14, 'waiting ...', {color:'#ffffff', fontSize:10})
        .setOrigin(0);

    let enemy1 = this.add.rectangle(60.25, 5, 8, 8, 0xfff000)
        .setOrigin(0.5);
    let player = this.add.rectangle(60, 20, 8, 8, 0xffffff);
    
    layer.setCollision([14]);
    
    this.matter.add.gameObject(enemy1);
   
    this.matter.add.gameObject(player);
    this.matter.world.convertTilemapLayer(layer, {label:'floor'});
    
    player.body.label = 'player';
    enemy1.body.label = 'enemy';
    
    this.input.on('pointerup', _=> player.setVelocityY(-3))
    
    player.setVelocityY(-1)

    this.matter.world.on("collisionactive", (e, o1, o2) => {

        let text = 'p: ';
        
        if( e.pairs.some (pair => pair.bodyA.label == 'player' && pair.bodyB.label =='floor' )) {
            text += 'touch floor ';
        }
        
        if( e.pairs.some (pair => pair.bodyA.label == 'enemy' && pair.bodyB.label =='player' )) {
            text += 'touch enemy ';
        }
        
        if( e.pairs.some (pair => pair.bodyA.label == 'enemy' && pair.bodyB.label =='floor' )) {
            text += '\ne: touch floor';
        }
        info.setText(text);            
    });
}

new Phaser.Game(config);
<script src="//cdn.jsdelivr.net/npm/phaser@3.55.2/dist/phaser.js"></script>