以下几行源代码(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上面的编码吗?
答案 0 :(得分: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&LT; Tuna *&gt;(dataA) - 如果dataA不是Tuna实例,则dynamic_cast返回NULL