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;
}
4

2 回答 2

2

它是box2d。在较新的版本中,我相信有一个名为 b2EdgeShape 的类,它有一个名为 Set() 的方法,您可以使用它来代替多边形形状及其已弃用的 setEdge 方法。

http://www.box2d.org/manual.html

见第 4.5 节

于 2012-01-19T02:59:44.967 回答
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);
于 2012-01-19T23:04:02.453 回答