How to Create Collision detection that could gave different output base on the target it collided with?

How to Create Collision detection that could gave different output base on the target it collided with?
0

Hi, im new to cocos creator. I’m trying to create a collision detection function that could give out different output when the target collided with different object. below are my code which i wrote base on the example from the Cocos tutorial site.

cc.Class({
    extends: cc.Component,

    properties: {
        target:{
            type: cc.Node,
            default: null

        },

        sprite1:{
            type: cc.Node,
            default: null,
    
          },
    
          sprite2:{
            type: cc.Node,
            default: null,
          },
    
          sprite3:{
            type: cc.Node,
            default: null,
          }
    },

    // LIFE-CYCLE CALLBACKS:

    onLoad () {
        
        cc.director.getCollisionManager().enabledDebugDraw=true;
        cc.director.getCollisionManager().enabled=true;
        cc.director.getCollisionManager().enabledDrawBoundingBox=true;
        
        
    },
  

    onCollsionEnter(sprite1, target){
        console.log("sprite 1 hit");

    },

    onCollsionEnter(sprite2, target){
        console.log("sprite 2 hit");

    },
    start () {
        
    },

    // update (dt) {},
}); 

Several ways… you can use a tagging system.

if (target->getTag == SOME_TAG) { //perform some function} else if (target->getTag == SOME_OTHER_TAG) { //perform some other function
(switch/case better here with int/enum…

A better way would be to create a well structured class hierarchy of game nodes, where a virtual collision method would be handled by the specific game node subclasses etc… virtual void collidedWith(target)

target->collidedWith(...
sprite1->collidedWith(...