动作游戏 主角的攻击Box 碰到 敌人的 受伤box 的时候会调用2次 onCollisionEnter 方法 ,这是为什么?怎么解决
onCollisionEnter(other:cc.BoxCollider,self:cc.BoxCollider){
// if(this.tag && this.currentCollider == other) return;
if(this.tag) return;
this.tag = true;
let otherNode = other.node.parent;
let selfNode= self.node.parent;
selfNode.active = false;
let attacker = other.node.parent;
let attackCharactor:Charactor = null;
if (other.node.group=="pa"){
attackCharactor = attacker.getComponent("HeroCtrol") as HeroCtrol;
console.log("玩家打怪物");
}else if(other.node.group=="ea"){
attackCharactor = attacker.getComponent("EnemyCtrol") as EnemyCtrol;
console.log("怪物打玩家");
}else{
return;
}
console.log("attackCharactor==>"+attackCharactor);
let attackNum = attackCharactor.getAttack();
let hurter = self.node.parent;
let hurtCharacotr:Charactor = null;
if(self.node.group=="ph"){
hurtCharacotr = hurter.getComponent("HeroCtrol") as HeroCtrol;
}else if (self.node.group=="eh"){
hurtCharacotr = hurter.getComponent("EnemyCtrol") as EnemyCtrol;
}else{
return;
}
let def = hurtCharacotr.getDef();
//计算伤害
console.log("attackNum==>"+attackNum+"def==>"+def);
let damageNum = MyGameMathf.getInstance().calculatingDamage(attackNum , def);
//调用受伤害物体的 hurt 方法
hurtCharacotr.damage(damageNum);
selfNode.active = true;
this.tag = false;
}