在cocos2d-2.0-x-2.04 版本中,创建一个box2d工程,其中的默认代码中,定义了一个PhysicsSprite类,按道理精灵与刚体绑定的时候需要将精灵的指针赋给刚体的userdata属性,他们才能关联起来,但是模板代码中并没有这么做,而是刚体赋给精灵的一个属性,但是照样工作正常,我很疑惑,希望大侠指点啊
下面是添加精灵的方法,我很纳闷不知道怎么绑定的求解
void HelloWorld::addNewSpriteAtPosition(CCPoint p)
{
CCLOG(“Add sprite %0.2f x %02.f”,p.x,p.y);
CCNode* parent = getChildByTag(kTagParentNode);
//We have a 64x64 sprite sheet with 4 different 32x32 images. The following code is
//just randomly picking one of the images
//int idx = (CCRANDOM_0_1() > .5 ? 0:1);
//int idy = (CCRANDOM_0_1() > .5 ? 0:1);
PhysicsSprite *sprite = new PhysicsSprite();
sprite->initWithTexture(m_pSpriteTexture, CCRectMake(0,0,128,128));
sprite->autorelease();
parent->addChild(sprite);
sprite->setPosition( CCPointMake( p.x, p.y) );
// Define the dynamic body.
//Set up a 1m squared box in the physics world
b2BodyDef bodyDef;
bodyDef.type = b2_dynamicBody;
bodyDef.position.Set(p.x/PTM_RATIO, p.y/PTM_RATIO);
b2Body *body = world->CreateBody(&bodyDef);
// Define another box shape for our dynamic body.
//b2PolygonShape dynamicBox;
//dynamicBox.SetAsBox(.5f, .5f);//These are mid points for our 1m box
b2CircleShape circle;
circle.m_radius =64.0/PTM_RATIO;
// Define the dynamic body fixture.
b2FixtureDef fixtureDef;
fixtureDef.shape = &circle;
fixtureDef.density = 1.0f;
fixtureDef.friction = 0.3f;
fixtureDef.restitution=0.8f;
body->CreateFixture(&fixtureDef);
sprite->setPhysicsBody(body);
}
