SetAsEdge问题Cocos 2D

时间:2011-10-11 06:04:03

标签: iphone cocos2d-iphone

我正在尝试用Cocos 2D& amp;方框2D

但是当我尝试设置“setAsEdge”时,它只是说我的功能不存在

我的代码:

// Define the ground body.
    b2BodyDef groundBodyDef;
    groundBodyDef.position.Set(0, 0); // bottom-left corner

    // Call the body factory which allocates memory for the ground body
    // from a pool and creates the ground box shape (also from a pool).
    // The body is also added to the world.
    groundBody = world->CreateBody(&groundBodyDef);

    // Define the ground box shape.
    b2PolygonShape groundBox;       

    // bottom
    groundBox.SetAsEdge(b2Vec2(0,FLOOR_HEIGTH/PTM_RATIO), b2Vec2(screenSize.width*2.0f/PTM_RATIO,FLOOR_HEIGTH/PTM_RATIO));
    groundBody->CreateFixture(&groundBox,0);

    // top
    groundBox.SetAsEdge(b2Vec2(0,screenSize.height/PTM_RATIO), b2Vec2(screenSize.width*2.0f/PTM_RATIO,screenSize.height/PTM_RATIO));
    groundBody->CreateFixture(&groundBox,0);

    // left
    groundBox.SetAsEdge(b2Vec2(0,screenSize.height/PTM_RATIO), b2Vec2(0,0));
    groundBody->CreateFixture(&groundBox,0);

    // right
    groundBox.SetAsEdge(b2Vec2(screenSize.width*2.0f/PTM_RATIO,screenSize.height/PTM_RATIO), b2Vec2(screenSize.width*2.0f/PTM_RATIO,0));
    groundBody->CreateFixture(&groundBox,0);

错误:'b2PolygonShape'中没有名为'SetAsEdge'的成员。我正在使用Cocos 2D 1.0.1 Stable

感谢所有人

3 个答案:

答案 0 :(得分:17)

您正在使用Box2D 2.2.1,与Cocos2D v1.0.1中的Box2D版本相比,其API已更改(使用Box2D v2.1.2)。我认为你使用的是Cocos2D 2.0 alpha?如果是这样,它应该带有更新的Xcode项目模板,因此您可能希望安装这些模板并基于新模板创建项目。

以下是我使用附有4个形状的主体设置边框边界框的方法:

    // 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);

或者,获取Kobold2D预览5(明天可用)并查看Physics-Box2D模板项目。我做了所有必要的更改来运行Box2D v2.2.1应用程序,包括Box2D调试绘图类所需的更改。我还托管了最新的Box2D API reference

答案 1 :(得分:2)

使用b2EdgeShape而不是b2PolygonShape,例如:

b2EdgeShape groundBox;
groundBox.Set( b2Vec2(0,0), b2Vec2(winSize.width/PTM_RATIO,0));

答案 2 :(得分:1)

使用

setAsBox(0,0,b2vec2(0,0),0);