1
我將Box2D多邊形添加到我的世界,我無法弄清楚如何將紋理添加到多邊形的形狀。多邊形是一個三角形,並且在rect:參數中使用CGRectMake(),同時初始化我的精靈會給我一個大於我的多邊形的精靈。如何將CCSprite(紋理)添加到多邊形?
這是我的方法是,現場
-(void) addSpring:(zSpring*)spring
{
[self addChild:spring.sprite];
CGPoint p = spring.coords;
//static triangle one
b2Vec2 vertices[3];
int32 count = 3;
vertices[0].Set(0.0f,0.0f);
vertices[1].Set(2.0f,0.0f);
vertices[2].Set(0.0f,1.0f);
b2BodyDef springBodyDef;
springBodyDef.type = b2_staticBody;
springBodyDef.position.Set(p.x/PTM_RATIO ,p.y/PTM_RATIO);
springBodyDef.userData = spring.sprite;
b2Body *body = world->CreateBody(&springBodyDef);
b2PolygonShape polygon;
polygon.Set(vertices, count);
b2FixtureDef springShapeDef;
springShapeDef.shape = &polygon;
springShapeDef.density = 1.0f;
springShapeDef.friction = 0.2f;
springShapeDef.restitution = 1.6f;
body->CreateFixture(&springShapeDef);
}
內增加了多邊形(春季),這是方法,類,在那裏我開始彈簧和彈簧精靈之內。
-(id)initWithCoords:(CGPoint)p withSpringType:(int)st
{
self.springType = st;
self.coords = p;
CCTexture2D *texture = [[CCTextureCache sharedTextureCache] addImage:@"metalTexture.png"];
// When initializing the sprite I want to make a polygon (triangle), not a rectangle
self.sprite = [[CCSprite alloc] initWithTexture:texture rect:CGRectMake(0, 0, 32, 32)];
self.sprite.position = ccp(p.x, p.y);
self.sprite.tag = 2;
return self;
}
如何使用紋理初始化多邊形的精靈?並只使多邊形的形狀具有紋理?謝謝!