2

我想从精灵设置身体的用户数据。但身体没有显示在屏幕上。但它不起作用。我不知道为什么。我的代码如下所示:如果您知道问题出在哪里,请告诉我。谢谢!

 CGSize screenSize = [[CCDirector sharedDirector] winSize];
    CGPoint screenCenter = ccp(screenSize.width*0.5f, screenSize.height*0.5f);

    b2Vec2 gravity = b2Vec2(0.0f, -10.0f);
    bool allowBodiesToSleep = true;
    world = new b2World(gravity);
    world->SetAllowSleeping(allowBodiesToSleep);

    b2BodyDef bodyDef;
    bodyDef.type = b2_dynamicBody;
    bodyDef.position = [self toMeters:screenCenter];

    CCSprite *hero = [CCSprite spriteWithFile:@"hero_jump0.png"];
    hero.position = screenCenter;

    bodyDef.userData = hero;

    b2Body *body = world->CreateBody(&bodyDef);

    b2PolygonShape dynamicBox;
    dynamicBox.SetAsBox(hero.contentSize.width/PTM_RATIO*0.5f, hero.contentSize.height/PTM_RATIO*0.5f);

    b2FixtureDef fixtureDef;
    fixtureDef.shape = &dynamicBox;
    fixtureDef.density = 0.3f;
    fixtureDef.friction = 0.5f;
    fixtureDef.restitution = 0.6f;

    body->CreateFixture(&fixtureDef);
4

1 回答 1

2

您还没有将您的精灵作为孩子添加到您的节点层次结构中。所以它在范围的末尾被删除

于 2012-11-30T19:03:29.863 回答