vendredi 26 février 2016

Contact listener does not work correctly in cocos2dx

I have following cod

    auto contactListener = EventListenerPhysicsContact::create();
    contactListener->onContactBegin = CC_CALLBACK_1(HelloWorld::onContactBegin, this);
    _eventDispatcher->addEventListenerWithSceneGraphPriority(contactListener, this);

Here is onConcactBegin function

bool HelloWorld::onContactBegin(const cocos2d::PhysicsContact &contact)
{
    auto nodeA = contact.getShapeA()->getBody()->getNode();
    auto nodeB = contact.getShapeB()->getBody()->getNode();
    Size visibleSize = Director::getInstance()->getVisibleSize();
    Vec2 origin = Director::getInstance()->getVisibleOrigin();
    if (nodeA && nodeB)
    {
        int a=nodeA->getTag();
        int b=nodeB->getTag();
        if (nodeA->getTag() >=5)
        {
            nodeA->remveFromParrentAndCleanup(true);
        }
        else if (nodeB->getTag() >=5)
        {                
            nodeB->remveFromParrentAndCleanup(true);   
        }
        addSprite();
    }
}

Function addSprite() use to add a new Sprite it has same initial property with the node was removed. Problem is the after the first collision have happened, the new sprite was added to the Layer, however after the second Collision (collision between new Sprite with the sprite has not been removed ), no other sprite be added. When I comment out remveFromParrentAndCleanup() lines, the second collision happened as I expected. I think remveFromParrentAndCleanup() lines make collision not work properly any more.Please help me solve this problem, thank you very much

Aucun commentaire:

Enregistrer un commentaire