Cocos2d设置边缘问题

3
在cocos2d版本v1.0.1中
        groundBox.SetAsEdge(left,right);

需要避免使用SetAsEdge方法,因为该方法已在之前的版本中被删除,因此会出现错误提示该方法不存在。

但我不确定如何做到这一点,因为它不是创建一个盒子,我也不确定它是通过使用顶点数组来创建多条线段(根据我的理解),如何使用新的方法实现。

- (void)createGroundEdgesWithVerts:(b2Vec2 *)verts numVerts:(int)num 
                   spriteFrameName:(NSString *)spriteFrameName {
    CCSprite *ground = 
    [CCSprite spriteWithSpriteFrameName:spriteFrameName];
    ground.position = ccp(groundMaxX+ground.contentSize.width/2, 
                          ground.contentSize.height/2);
    [groundSpriteBatchNode addChild:ground];

    b2PolygonShape groundShape;  

    b2FixtureDef groundFixtureDef;
    groundFixtureDef.shape = &groundShape;
    groundFixtureDef.density = 0.0;

    // Define the ground box shape.
    b2PolygonShape groundBox;       

    for(int i = 0; i < num - 1; ++i) {
        b2Vec2 offset = b2Vec2(groundMaxX/PTM_RATIO + 
                               ground.contentSize.width/2/PTM_RATIO, 
                               ground.contentSize.height/2/PTM_RATIO);
        b2Vec2 left = verts[i] + offset;
        b2Vec2 right = verts[i+1] + offset;

        groundShape.SetAsEdge(left,right);

        groundBody->CreateFixture(&groundFixtureDef);    
    }

    groundMaxX += ground.contentSize.width;
}

你在谈论Box2D而不是Cocos2d。 - Bryan Chen
是的,从技术上讲,它们两者都是。 - Luke
2个回答

2
这是Box2D。在更新的版本中,我相信有一个叫做b2EdgeShape的类,并且它有一个叫做Set()的方法。你可以使用它代替多边形形状和它已经过时的setEdge方法。
请参见第4.5节。 http://www.box2d.org/manual.html

2
你可以查看一下新的Cocos2D+Box2D示例项目是如何做的。
以下是我在Kobold2D中创建一个全屏大小的盒子的方法:
    // for the screenBorder body we'll need these values
    CGSize screenSize = [CCDirector sharedDirector].winSize;
    float widthInMeters = screenSize.width / PTM_RATIO;
    float heightInMeters = screenSize.height / PTM_RATIO;
    b2Vec2 lowerLeftCorner = b2Vec2(0, 0);
    b2Vec2 lowerRightCorner = b2Vec2(widthInMeters, 0);
    b2Vec2 upperLeftCorner = b2Vec2(0, heightInMeters);
    b2Vec2 upperRightCorner = b2Vec2(widthInMeters, heightInMeters);

    // Define the static container body, which will provide the collisions at screen borders.
    b2BodyDef screenBorderDef;
    screenBorderDef.position.Set(0, 0);
    b2Body* screenBorderBody = world->CreateBody(&screenBorderDef);
    b2EdgeShape screenBorderShape;

    // Create fixtures for the four borders (the border shape is re-used)
    screenBorderShape.Set(lowerLeftCorner, lowerRightCorner);
    screenBorderBody->CreateFixture(&screenBorderShape, 0);
    screenBorderShape.Set(lowerRightCorner, upperRightCorner);
    screenBorderBody->CreateFixture(&screenBorderShape, 0);
    screenBorderShape.Set(upperRightCorner, upperLeftCorner);
    screenBorderBody->CreateFixture(&screenBorderShape, 0);
    screenBorderShape.Set(upperLeftCorner, lowerLeftCorner);
    screenBorderBody->CreateFixture(&screenBorderShape, 0);

怎么将它的颜色设置为透明,最好是透明的? - JqueryToAddNumbers

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接