如何向多边形添加CCSprite(纹理)?

发布于 2024-10-17 13:49:58 字数 1543 浏览 2 评论 0原文

我正在将 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

四叶草在未来唯美盛开 2024-10-24 13:49:58

不完全确定,但我认为您可能对 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...

  • Create the sprite with the texture
  • Create a collision poly that follows movement of the sprite and gets updated as the sprite position gets updated

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文