"onContactBegin" is called more than one time

"onContactBegin" is called more than one time
0.0 0

#1

Hello guys.
In documentation is said when a contact happens then the start of contact will be “onContactBegin”. In other words, “onContactBegin” should be called once. or I am wrong?

Other question: If I want “onContactBegin” called once then what is your suggestion?

Thanks


#2

Can you show us what you are doing?


#3
void InSceneManager::handlingContactsNormal() {
    contactListener = EventListenerPhysicsContact::create();
    contactListener->onContactBegin = [=] (PhysicsContact& contact) -> bool {
        log("contactListener");
        return contactBeging(contact);
    };

    currentScene->getEventDispatcher()->addEventListenerWithSceneGraphPriority(contactListener,
                                                                               currentScene);
}

bool InSceneManager::contactBeging(PhysicsContact &contact) {
    log("contactID : %i", contact._ID);

    int objectATag = contact.getShapeA()->getTag();
    int objectBTag = contact.getShapeB()->getTag();
    auto nodeA = contact.getShapeA()->getBody()->getNode();
    auto nodeB = contact.getShapeB()->getBody()->getNode();

    if ((objectATag == 1)  && (objectBTag == 1))
    {
        onContactBulletBullet(nodeA, nodeB, objectATag, objectBTag);
    } else if ((objectATag == 1)  && (objectBTag == 2) || (objectATag == 2)  && (objectBTag == 1))
    {
        onContactBulletFireFighter(nodeA, nodeB, objectATag, objectBTag);
    } else if ((objectATag == 1)  && (objectBTag == 3) || (objectATag == 3)  && (objectBTag == 1))
    {
        onContactBulletBigStone(nodeA, nodeB, objectATag, objectBTag);
    } else if ((objectATag == 2)  && (objectBTag == 3) || (objectATag == 3)  && (objectBTag == 2))
    {
        onContactBigStoneFireFighter(nodeA, nodeB, objectATag, objectBTag);
    }

    return true;
}

#4

And can I see the relevant log pieces too


#5

Here you are:

D/cocos2d-x debug info:
                       contactListener
                        contactID : 2503
                        contactBeging
                        printed
                        contactListener
                        contactID : 2504
                        contactBeging
                        printed

As you see the “contactBeginig” is printed twice while I just shot a bullet and it collisioned with target once and then disapeared.


#6

What version?

I have a physics based game I can test with to see if my events are getting called twice.


#7

3.17. That’s so good if you do that and inform Me.

PS: What is your game? you released that?


#8

Could you show me where you create and set the PhysicBody for your bullet ?


#9

Yup I plan to look at this today :slight_smile: a good weekend project.