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;
}
如何使用纹理初始化多边形的精灵?并只使多边形的形状具有纹理?谢谢!