Search code examples
box2dcocos2d-x

update on different type node


I am trying to draw body node type and also polygonesprite. when i am going to update both type on update method. i got issues.

This is polygone type:

BombBallSprite *bomb = BombBallSprite::spriteWithWorld(world);
bomb->getBody()->SetType(b2_staticBody);
bomb->activateCollisions();

this->addChild(bomb);

This is body node type:

 this->addBodyNode(node, 0);
 node->release();

This above two types are drawing in single scene.

When i update on both, i have issues:

 void HelloWorld::update(float dt)
 {
     if( gameState_ != kGameStatePaused )
     {
        world_->Step(dt, velocityIterations, positionIterations);
     }

     this->checkAndSliceObjects();

}


void HelloWorld::checkAndSliceObjects()
{
double curTime = getTimeTick();

for (b2Body* b = world_->GetBodyList(); b; b = b->GetNext())
{
    if (b->GetUserData() != NULL)
    {

        PolygonSprite *sprite = (PolygonSprite*)b->GetUserData();
        BodyNode *node = (BodyNode*) b->GetUserData();
        if( node && (node->properties_ & BN_PROPERTY_SPRITE_UPDATED_BY_PHYSICS) ) {

          -------

        }

        if (sprite->getSliceEntered() && curTime > sprite->getSliceEntryTime())
        {
            sprite->setSliceEntered(false);
        }
        else if (sprite->getSliceEntered() && sprite->getSliceExited())
        {
            this->splitPolygonSprite(sprite); 
        }                      
    }

}

}

I am clueless how will handle the followings on update method

    PolygonSprite *sprite = (PolygonSprite*)b->GetUserData();
    BodyNode *node = (BodyNode*) b->GetUserData();

Can any one assist me?


Solution

  • I have one short cut method.

    You have to set tag for all. E.g. polygone sprite for 100, sprite for 200.

    It is possible to check tag using user data. If the tag is 100, you will do polygone sprite update. If the tag is 200, you will do sprite update.

    I think it will help you.