1

我卡在这个位置,不知道这出了什么问题,

我在我的项目中启用了 ARC。我按如下方式制作了softBody

球.h

    B2Body *body[NUM_SEGMENT];
    CCSprite *ball;

球.mm

    ball = [CCSprite spriteWithFile:@"Ball1.2.png"];
    ball.tag = 1;


    for(int i=0;i<NUM_SEGMENT;i++){
        float theta = deltaAngle*i;
        float x = radius * cosf(theta);
        float y = radius * sinf(theta);
        b2Vec2 circlePosition = b2Vec2(x/PTM_RATIO,y/PTM_RATIO);
        b2BodyDef bodyDef;
        bodyDef.type = b2_dynamicBody;
        bodyDef.position = (center + circlePosition);
        bodyDef.userData = &ball;
        body[i] = world->CreateBody(&bodyDef);
        outerBodyFixture[i]=body[i]->CreateFixture(&fixtureDef);
        [bodies addObject:[NSValue valueWithPointer:body[i]]];
    }

我给瓷砖赋予了如下物理特性,

瓷砖.h

CCSprite *tile;

瓷砖.mm

tile = [layer1 tileAt:ccp(i, j)];
tile.tag = 0;
b2BodyDef tileDef;
tileDef.type = b2_staticBody;
tileDef.position.Set((tile.position.x+(tile.contentSize.width/2))/(PTM_RATIO), (tile.position.y + (tile.contentSize.height/2))/PTM_RATIO);
tileDef.userData = &tile;
tileBody = world->CreateBody(&tileDef);

现在我试图捕捉碰撞检测,我已经编写了代码来打印碰撞体的标签号。代码如下,

std::vector<MyContact>::iterator pos;
    for (pos=_contactListener->_contacts.begin();
         pos != _contactListener->_contacts.end(); ++pos) {
        MyContact contact = *pos;

        b2Body *bodyA = contact.fixtureA->GetBody();
        b2Body *bodyB = contact.fixtureB->GetBody();
        if (bodyA->GetUserData() != NULL && bodyB->GetUserData() != NULL) {

此时出现错误:EXC_BAD_ACCESS

        CCSprite *spriteA = (__bridge CCSprite *) bodyA->GetUserData();

此时出现错误:EXC_BAD_ACCESS

        CCSprite *spriteB = (__bridge CCSprite *) bodyB->GetUserData();


            printf("contact :%d \n",spriteB.tag);
        }
    }

不知道这段代码有什么问题,请给我一些解决方案

4

1 回答 1

0

您的问题是您存储在userData指向指针的指针而不是指针本身。

tile已经指向一个CCSprite实例并&tile指向一个指向CCSprite实例的指针,使您的转换不正确。

所以将其更改为:

tileDef.userData = tile;
于 2013-05-06T08:47:19.727 回答