-1

下面几行源码(cocos2d)

BodyNode *dataA = (BodyNode*) bodyA->GetUserData(); 
BodyNode *dataB = (BodyNode*) bodyB->GetUserData();

Class p1 = [Tuna class];
Class p2 = [Oilbarrel class];
Class p3 = [Barrelhorizontal class];


if( [dataA isKindOfClass:p1] ||[dataB isKindOfClass:p1] 
   || [dataA isKindOfClass:p2] ||[dataB isKindOfClass:p2]
   || [dataA isKindOfClass:p3] ||[dataB isKindOfClass:p3]
   ) 
{

        state1 = tBomb1Hit;
}

你能帮我改一下cocos2dx中的上述编码吗?

4

1 回答 1

7

您需要使用 dynamic_cast

    if( dynamic_cast<Tuna*>(dataA) || dynamic_cast<Tuna*>(dataB)
     ||dynamic_cast< Oilbarrel*>(dataA) || dynamic_cast< Oilbarrel*>(dataB)
     ||dynamic_cast< Barrelhorizontal*>(dataA) || dynamic_cast< Barrelhorizontal*>(dataB) ){
        state1 = tBomb1Hit;

}

dynamic_cast< Tuna* >(dataA) - 如果 dataA 不是 Tuna 实例,则 dynamic_cast 返回 NULL

于 2013-09-03T09:24:39.690 回答