如何向多边形添加CCSprite(纹理)?
我正在将 Box2D 多边形添加到我的世界中,但我不知道如何仅向多边形形状添加纹理。多边形是一个三角形,在初始化我的精灵时使用 CGRectMake() 作为 rect: 参数给我一个比我的多边形更大的精灵。
这是我在场景中添加多边形(弹簧)的方法
-(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;
}
如何为多边形初始化带有纹理的精灵?并且只让多边形的形状有纹理?谢谢!
I am adding Box2D polygon's to my world and I can't figure out how to add texture to only the polygon shape. The polygon is a triangle and using the CGRectMake() for the rect: parameter while initializing my sprite gives me a sprite larger then my polygon.
This is my method that adds the polygon (spring) within the scene
-(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);
}
and this is the method, within the class, where I initiate the spring and the springs sprite.
-(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;
}
How do I initialize a sprite, with a texture, for a polygon? And make only the shape of the polygon have the texture? Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
不完全确定,但我认为您可能对 Cocos2D 中纹理的使用和 Box2D 中碰撞空间的使用有点困惑。除非您开始了解纹理坐标的一些细节,否则您无法将精灵的纹理应用到碰撞多边形的精确范围,但我认为这不会是您想要的结果。通常会做什么...
希望我不会对您不知道的事情做太多假设。如果这有帮助或者您有任何其他问题,请告诉我。
Not entirely sure but I think you may be a bit confused on the use of textures in Cocos2D and collision space in Box2D. You can't have the texture for the sprite be applied to the exact extents of the collision polygon unless you start getting into some of the specifics of texture coordinates but I don't think this will be the result you want. Usually what is done...
Hope I'm not assuming to much in what you don't know here. Let me know if this helps or if you have any other questions.