How can I use tag from Collider Component?

Hi,

I’m using the simple method for colliding between objects in my game, like the following:

properties: {
zombiecolliderobjects:{
default:[],
type: [cc.Prefab]
},
},

onCollisionEnter:function(other,self){
this.node.color = cc.Color.RED;
this.touchingNumber ++;
if(cc.rectIntersectsRect(this.node.getBoundingBox(),this.zombiecolliderobjects[0]))
{this.ZombieBulletLeftHit()}

However, how can I use tag for colliding objects? The tag that’s in the box collider component? Or is it possible? Would appreciate any help…Thanks and God Bless…

Sincerely,

Sunday

Tag is used to distinct several colliders from the same node.
You can simply set tag in Creator inspector, and get the tag from the collider.

Here is the example :


Hi @jyinkailej

Thanks for the example, really appreciated it…But what I’m trying to accomplish is to use the tag behavior to differentiate between objects / node that come in contact… Can I use a combination of tag with cc.recIntersectsRect for colliding objects, like below:

if(cc.rectIntersectsRect(this.node.getBoundingBox(),this.zombiecolliderobjects[0].tag===3))
{this.ZombieBulletLeftHit()}

I have the below array:

properties: {
zombiecolliderobjects:{
default:[],
type: [cc.Prefab]
},

Which is essentially prefabs / objects that can collide with the main node, being leftzombie. So, I’m trying to make the collision just collide with specified objects as per collider tag, not with every object in the scene…Thanks again for help and God Bless…

Sincerely,

Sunday

I think what you need is group.

You can edit Group Collide Map in : Project Settings -> Group Manager.

Then choose a group for the node.

Only collider pairs will collide with each other.

1 Like

Hi @jyinkailej

Thanks for the quick explanation, much appreciation… :smile:
Yes, I’ve set up already the collider groups and assigned them individually to their groups within their nodes.…However, let’s say, I have a collider group by the name of zombies, then I have a wall and I have a lightning, so I’ve checked under Group Collide Map - > zombies - > wall - > lightning… In other words, zombies should collide with wall and lightning as per checked…So far so good…Nevertheless, when I apply the simple method of colliding using getBoundingBox like above without the tag… When zombies hit the wall, is fine, but within the zombie node, I also have a method collider set for Lightning, which is only supposed to fire when zombies collide with lightning and not wall…I hope you understand… Thanks again for the help…God Bless…

Sincerely,

Sunday

Hi @jyinkailej

I’ve already resolved it, it was a programming issue… :grinning: Thanks again for the help…God Bless…

Sincerely,

Sunday

Hi @pandamicro

Nope…I tried changing some programming issue, but it’s not solved yet, please look below:

zombiecolliderobjects:{
default:[],
type: [cc.Prefab]
},

I have an array of 4 prefabs [0], [1], [2], [3]

onCollisionEnter:function(other,self){
this.node.color = cc.Color.RED;
this.touchingNumber ++;
//Left Zombie Collides with Left Bullet–Function//
if(cc.rectIntersectsRect(this.node.getBoundingBox(),this.zombiecolliderobjects[0]))
{this.ZombieBulletLeftHit()}
//Left Zombie Collides with Right Bullet–Function//
if(cc.rectIntersectsRect(this.node.getBoundingBox(),this.zombiecolliderobjects[1]))
{this.ZombieBulletLeftHit()}
//Left Zombie Collides with U Lightning–Function//
if(cc.rectIntersectsRect(this.node.getBoundingBox(),this.zombiecolliderobjects[2]))
{this.ZombieLightningHitBurn()}
//Left Zombie Collides with The U Tornado --Function
if(cc.rectIntersectsRect(this.node.getBoundingBox(),this.zombiecolliderobjects[3]))
{this.ZombieLightningHitBurn()}

I’ve set up the Group Colliders via the panel and within each node ( group ), placed checked marks for collision within the panel, and added the above code… So, the issue that I’m having is that example, if this.node (zombie ) collides with this.zombiecolliderobjects[0] ( bullet ) it’s supposed to call method {this.ZombieBulletLeftHit()} , but it doesn’t, it calls method {this.ZombieLightningHitBurn()}. I added else at the end of each if to see if it would solve the issue, but it didn’t work…Would appreciate any feedback…Thanks and God Bless…

Sincerely,

Sunday

Hi,

Taking a break from working on Unity, going back to Cocos Creator, for those newbies in Cocos Creator, regarding Colliding different nodes / objects and invoking their respective methods, I found a solution:

onCollisionEnter:function**(other,self)**{
this.node.color = cc.Color.RED;
this.touchingNumber ++;
//Collides with Bullet Left- Destroy Zombie Function //
if(other.node.name==“bulletprefableft”)
{this.ZombieBulletLeftHit()}

So, instead of using Tag, which I don’t know, if it could be use, I used the name of the node to differentiate the methods invoking (ex: player hits bullet, player eats food, etc…) . Hopefully this can help any newbie entering into using Cocos Creator…Thanks and God Bless…

Sincerely,

Sunday