Phaser Matter Collision Plugin 碰撞插件 -- iFiero技术分享

collision-simple-demo

Phaser 自带的Arcade虽然易用,但复杂的物理碰撞明显就不够用了,于是Matter等物理引擎还是不得不学的,以下是Matter物理体碰撞的一个插件,它省去了我们判别两个物理体相撞后哪个是gameObjectA和gameObjectB,实在是好用又省心,特推荐如下:

引入插件:

const config = {
  // ...
  physics: {
    default: "matter"
  },
  // Install the scene plugin
  plugins: {
    scene: [
      {
        plugin: PhaserMatterCollisionPlugin, // The plugin class
        key: "matterCollision", // Where to store in Scene.Systems, e.g. scene.sys.matterCollision
        mapping: "matterCollision" // Where to store in the Scene, e.g. scene.matterCollision
      }
    ]
  }
};
 
const game = new Phaser.Game(config);

识别二个相撞体

const player = this.matter.add.sprite(0, 0, "player");
const trapDoor = this.matter.add.image(200, 0, "door");
 
this.matterCollision.addOnCollideStart({
  objectA: player,
  objectB: trapDoor,
  callback: function(eventData) {
    // This function will be invoked any time the player and trap door collide
    const { bodyA, bodyB, gameObjectA, gameObjectB, pair } = eventData;
    // bodyA & bodyB are the Matter bodies of the player and door respectively
    // gameObjectA & gameObjectB are the player and door respectively
    // pair is the raw Matter pair data
  },
  context: this // Context to apply to the callback function
});

传感器

const player = this.matter.add.sprite(0, 0, "player");
const sensor = this.matter.world.add.rectangle(100, 0, 50, 50, { isStatic: true, isSensor: true });
 
this.matterCollision.addOnCollideStart({
  objectA: player,
  objectB: sensor,
  callback: eventData => console.log("Player touched hidden sensor")
});

Matter:http://brm.io/matter-js/
插件:https://www.npmjs.com/package/phaser-matter-collision-plugin
更多游戏教学:www.iFIERO.com -- 为游戏开发深感自豪

©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 我还未落魄到流浪街头 我还可以平静的看着人来人往 不羡慕,不鄙视,不探究 所以,我并不是在游荡 所以,我完全可以辨...
    青稞饼干阅读 1,176评论 0 0
  • 是格格不入么 不想还是不好意思 不要那么挑剔了 没有完美的人 相互磨合吧
    爱读书的阿瑞_阅读 1,579评论 0 0
  • 每年立春开始就有一种重新开始的冲动,暗下决心开始跑步读书,一晃到春分了,柳枝从枝干泛青到抽出嫩芽,樱花杏花纷...
    可乐汩汩阅读 2,635评论 0 0
  • 第12集 张:又找我! 苏:我能进去说吗! 张:不能,我爸还没醒呢!再说了,你进去了,我爸容易激动! 苏:我输了!...
    沈春阳粉丝团阅读 2,586评论 0 0