如何在UserData结构中包含/检索/设置正文的位置?

时间:2012-02-23 16:57:03

标签: struct cocos2d-iphone box2d

我设法为UserData创建了一个结构,但是当我从使用

切换时
ballBodyDef.userData = _ball;

MyBodyData *bodyData = new MyBodyData();
bodyData->someNumber = 4;
bodyData->sprite = _ball;
_body->SetUserData(bodyData);

以下失败:

for(b2Body *b = world->GetBodyList(); b; b=b->GetNext()) {    
    {
        if (b->GetUserData() != NULL)
        {
            CCSprite *myActor = (CCSprite*)b->GetUserData();
            myActor.position = CGPointMake(
                                           b->GetPosition().x * PTM_RATIO,
                                           b->GetPosition().y * PTM_RATIO );
            myActor.rotation = -1 * CC_RADIANS_TO_DEGREES(b->GetAngle());
        }
    }

我确定它与编译器无法找到/设置myActor的位置这一事实有关,但我不知道如何使用结构包含/检索/设置正文的位置...

1 个答案:

答案 0 :(得分:1)

 CCSprite *myActor = (CCSprite*)b->GetUserData();

您无法将MyBodyData*投射到CCSprite*。你必须写:

if (b->GetUserData() != NULL)
{
    MyBodyData *data = static_cast<MyBodyData*>(b->GetUserData());
    //then retrive any info you need from data
}