CCSprite不位于b2body之上

时间:2013-06-24 17:54:56

标签: box2d cocos2d-x

我在将ccsprite定位于b2body时遇到了麻烦。

我想在box2d主体上对齐ccsprite。现在它们之间存在差距,我不想要这个差距。

我正在使用Andreas Loew的GB2ShapeCache来获取身体上的装置。

enter image description here

PTM_RATIO = 32

这是我的代码:

string stageName="Stage1";

CCSize s = CCDirector::sharedDirector()->getWinSize();
CCPoint center=ccp((s.width/2),(s.height/2));

CCSprite *sprite = new CCSprite();
sprite->initWithFile("Stage1.png");
sprite->setAnchorPoint(CCPoint(0.50, 0.5));
sprite->setPosition(center);
this->addChild(sprite);

// Define the dynamic body.
//Set up a 1m squared box in the physics world
b2BodyDef bodyDef;
bodyDef.type = b2_dynamicBody;
bodyDef.userData=sprite;
bodyDef.position.Set(center.x/PTM_RATIO,center.y/PTM_RATIO);
//bodyDef.position=b2Vec2(10, 10);

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

//adding fixtudre to body
GB2ShapeCache::sharedGB2ShapeCache()->addFixturesToBody(body, "Stage1");

//weld joint at center
b2WeldJointDef *weldJoint= new b2WeldJointDef();
weldJoint->Initialize(groundBody, body, b2Vec2(center.x/PTM_RATIO,center.y/PTM_RATIO));
world->CreateJoint(weldJoint);

1 个答案:

答案 0 :(得分:1)

我找到了我的精灵图像在所有4个边上都有尾随空格的解决方案。一旦我删除它并使用物理编辑器重新构建多边形,我的问题就解决了。代码完全正常问题与图像