[]matter中有没有类似于unity中的OnCollision碰撞检测函数呀,用js怎么写

已邀请:

冰水blue

赞同来自:

么有。这个我给你个demo,你自己看看。
1.初始化
this.Matter.Events.on(this._engine, 'collisionActive', this.onCollision);
 
2.碰撞检测
private onCollision(event): void
{
console.log("碰撞了.."); 
var home = _gamePage._mainPage._playPage; 
for(var i = 0; i < event.pairs.length; i++) { 
var pair = event.pairs[i]; 
if(!(pair.bodyA.label === 'gun' || pair.bodyB.label == "gun")) continue; 
var other; 
if (pair.bodyA.label === 'gun')
{
other = pair.bodyB;
}
else
{
other = pair.bodyA;

switch(other.label)
{
case "gameover":
{
home.onGameOver();
}
break; 
case "coin":
{
home.gainCoin(1);
}
break; 
case "bullet":
{
home.gainBullet(1);
}
break;

console.log("删除物体:", other.label); 
home.Matter.World.remove(home._engine.world, other);
}
}

(^_^)

赞同来自:

你的private onCollision(event)中的event与pairs是怎么赋值的呀

冰水blue

赞同来自:

这个碰撞,不怎么好,不建议使用刚体matter.js做碰撞检测。并且和unity差太远了。。。。。

要回复问题请先

商务合作
商务合作