开发者

SetAsEdge problems Cocos 2D

开发者 https://www.devze.com 2023-04-11 22:37 出处:网络
I am trying to make a game in Cocos 2D & Box 2D but when i try to put a \"setAsEdge\" it just said me that the function dosn\'t exist

I am trying to make a game in Cocos 2D & Box 2D

but when i try to put a "setAsEdge" it just said me that the function dosn't exist

my code:

// 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));
  开发者_如何学C  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);

Error: No member named 'SetAsEdge' in 'b2PolygonShape'. I am using Cocos 2D 1.0.1 Stable

Thanks to everyone


You are working with Box2D 2.2.1 whose API has changed compared to the Box2D version in Cocos2D v1.0.1 (uses Box2D v2.1.2). I take it you're using Cocos2D 2.0 alpha? If so, it supposedly comes with updated Xcode project templates, so you might want to install these and create a project based on the new templates.

Here's how I set the border bounding box by using a body with 4 shapes attached to it:

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

Alternatively, get Kobold2D Preview 5 (available by tomorrow) and have a look at the Physics-Box2D template project. I made all the necessary changes to run a Box2D v2.2.1 app, including the changes needed for the Box2D debug drawing class. I'm also hosting the latest Box2D API reference.


Use b2EdgeShape instead of b2PolygonShape, for example:

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


use

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

精彩评论

暂无评论...
验证码 换一张
取 消