2011-10-11 54 views
1

我试图在科科斯2D游戏&箱2DSetAsEdge问题科科斯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'的成员。我使用科科斯2D 1.0.1稳定

感谢大家

回答

17

您正在使用的Box2D 2.2.1,其API已经改变相比Box2D的版本在cocos2d V1.0.1(使用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(可在明天),并看看在物理,Box2D的模板项目。我做了所有必要的更改以运行Box2D v2.2.1应用程序,包括Box2D调试绘图类所需的更改。我还托管最新的Box2D API reference

1

使用

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

使用b2EdgeShape而不是b2PolygonShape,例如:

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