2011-02-15 46 views
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; 
} 

如何使用紋理初始化多邊形的精靈?並只使多邊形的形狀具有紋理?謝謝!

回答

0

不完全確定,但我想你可能會對Cocos2D和Box2D中的碰撞空間中的紋理使用感到困惑。除非您開始深入瞭解紋理座標的某些細節,否則不能將紋理應用於碰撞多邊形的確切範圍,但我不認爲這將是您想要的結果。通常什麼做...

  • 具有質感
  • 創建跟隨精靈的運動和精靈的位置被更新

希望我」被更新的碰撞聚創建精靈在這裏你不瞭解的東西太多。讓我知道這是否有幫助,或者如果您有任何其他問題。