LukeYe
LukeYe

Reputation: 25

cocos2D-Box2D,CCSprite not show on the screen

i want to set the body's userdata from a sprite.but the body not show on the screen. But it doesn't work.and i don`t know why. Here what my code looks like : if you know where is the problem, please tell me. Thanks!

 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);

Upvotes: 2

Views: 188

Answers (1)

Andrew
Andrew

Reputation: 24846

You have not added your sprite as a child into your node hierarchy. So it is deleted at the end of the scope

Upvotes: 2

Related Questions