我想用26个英文字符创建精灵,让它们在框架内相互碰撞(例如,如下图中的X和E)。
创建26个形状的推荐方法是什么?
答案 0 :(得分:4)
你为字母表的每个字符拍摄26张图像精灵(优先选择.png图像),然后定义每个角色的固定点,(借助于vertexHelper参考THIS链接了解更多关于 vertextHelper ),就像“我”一样:
//OBSTACLE_Adjustments
{
CCSprite *sprite = [CCSprite spriteWithFile:@"I.png"];
sprite.position = ccp(96,96);
sprite.tag = 2;
[self addChild:sprite];
//SHAPE 1:
{
b2BodyDef bodyDefinition;
bodyDefinition.type = b2_dynamicBody;
bodyDefinition.position.Set(96/PTM_RATIO, 96/PTM_RATIO);
bodyDefinition.userData = sprite;
obstacle_AdjustmentsBody= world->CreateBody(&bodyDefinition);
// Create body shape
b2PolygonShape bodyShape;
//row 1, col 1
int num = 4;
b2Vec2 verts[] = {
b2Vec2(6.5f / PTM_RATIO, -25.0f / PTM_RATIO),
b2Vec2(6.5f / PTM_RATIO, 27.0f / PTM_RATIO),
b2Vec2(-7.5f / PTM_RATIO, 27.0f / PTM_RATIO),
b2Vec2(-7.5f / PTM_RATIO, -26.0f / PTM_RATIO)
};
bodyShape.Set(verts, num);
// Create shape definition and add to body
b2FixtureDef bodyFixtureDefinition;
bodyFixtureDefinition.shape = &bodyShape;
bodyFixtureDefinition.density = 100.0f;
bodyFixtureDefinition.friction = 0.01f;
bodyFixtureDefinition.restitution = 0.50f;
obstacle_AdjustmentsFixture=obstacle_AdjustmentsBody->CreateFixture(&bodyFixtureDefinition);
}
}
此处obstacle_AdjustmentsBody
为b2Body
,obstacle_AdjustmentsFixture
为b2Fixture
。
这里是X的形状:
//OBSTACLE_ALPHABATE
{
CCSprite *sprite = [CCSprite spriteWithFile:@"X.png"];
sprite.position = ccp(96,96);
sprite.tag = 2;
[self addChild:sprite];
//SHAPE FIRST LEG OF X:
{
b2BodyDef bodyDefinition;
bodyDefinition.type = b2_dynamicBody;
bodyDefinition.position.Set(96/PTM_RATIO, 96/PTM_RATIO);
bodyDefinition.userData = sprite;
obstacle_AdjustmentsBody= world->CreateBody(&bodyDefinition);
// Create body shape
b2PolygonShape bodyShape;
//row 1, col 1
int num = 4;
b2Vec2 verts[] = {
b2Vec2(108.0f / PTM_RATIO, -71.5f / PTM_RATIO),
b2Vec2(56.0f / PTM_RATIO, 2.5f / PTM_RATIO),
b2Vec2(47.0f / PTM_RATIO, -3.5f / PTM_RATIO),
b2Vec2(96.0f / PTM_RATIO, -75.5f / PTM_RATIO)
};
bodyShape.Set(verts, num);
// Create shape definition and add to body
b2FixtureDef bodyFixtureDefinition;
bodyFixtureDefinition.shape = &bodyShape;
bodyFixtureDefinition.density = 100.0f;
bodyFixtureDefinition.friction = 0.01f;
bodyFixtureDefinition.restitution = 0.50f;
obstacle_AdjustmentsFixture=obstacle_AdjustmentsBody->CreateFixture(&bodyFixtureDefinition);
}
//SHAPE SECOND LEG OF X:
{
b2BodyDef bodyDefinition;
bodyDefinition.type = b2_dynamicBody;
bodyDefinition.position.Set(96/PTM_RATIO, 96/PTM_RATIO);
bodyDefinition.userData = sprite;
obstacle_AdjustmentsBody= world->CreateBody(&bodyDefinition);
// Create body shape
b2PolygonShape bodyShape;
//row 1, col 1
int num = 4;
b2Vec2 verts[] = {
b2Vec2(117.0f / PTM_RATIO, -4.5f / PTM_RATIO),
b2Vec2(113.0f / PTM_RATIO, -1.5f / PTM_RATIO),
b2Vec2(54.0f / PTM_RATIO, -51.5f / PTM_RATIO),
b2Vec2(61.0f / PTM_RATIO, -56.5f / PTM_RATIO)
};
bodyShape.Set(verts, num);
// Create shape definition and add to body
b2FixtureDef bodyFixtureDefinition;
bodyFixtureDefinition.shape = &bodyShape;
bodyFixtureDefinition.density = 100.0f;
bodyFixtureDefinition.friction = 0.01f;
bodyFixtureDefinition.restitution = 0.50f;
obstacle_AdjustmentsFixture=obstacle_AdjustmentsBody->CreateFixture(&bodyFixtureDefinition);
}
}