0

我只调用了 body->CreateFixture(&fixtureDef) 一次,但 Testbed 继续创建实体。这是我的代码:

#ifndef FOOTEST_H
#define FOOTEST_H
#define DEGTORAD 0.0174532925199432957f
#define RADTODEG 57.295779513082320876f    

class FooTest : public Test
    {
        public:
        FooTest() { } //do nothing, no scene yet

        void Step(Settings* settings)
        {
            //run the default physics and rendering
            Test::Step(settings);

        b2BodyDef myBodyDef;
        myBodyDef.type = b2_dynamicBody;
        myBodyDef.position.Set(0, 20);
        b2Body *dynamicBody = m_world->CreateBody(&myBodyDef);

        b2PolygonShape polygonShape;
        b2FixtureDef myFixtureDef;
        myFixtureDef.shape = &polygonShape;
        myFixtureDef.density = 1;

        for(int i=0;i<4;i++){
        b2Vec2 pos(sinf(i*90*DEGTORAD), cos(i*90*DEGTORAD));
        polygonShape.SetAsBox(1, 1, pos, 0);
        dynamicBody->CreateFixture(&myFixtureDef);
        }


        myBodyDef.type = b2_staticBody;
        myBodyDef.position.Set(0, 0);

        b2Body *staticBody = m_world->CreateBody(&myBodyDef);

        b2EdgeShape edgeShape;
        edgeShape.Set( b2Vec2(-15,0), b2Vec2(15,3) );

        myFixtureDef.shape = &edgeShape;
        staticBody->CreateFixture(&myFixtureDef);
        }

        static Test* Create()
        {
            return new FooTest;
        }
    };  
#endif

我正在关注本教程: iForce2D 本教程的屏幕截图仅显示了已创建夹具的一个实例。我在这里错过了什么吗?

4

1 回答 1

2

“Testbed”Step反复调用,您在每次调用中创建实例。
请注意,本教程在FooTest构造函数中进行了所有设置,因此它只运行一次。

Step出于某种原因,您已将设置从构造函数移至函数。
就在那篇教程的前面一点,它说“如果你没有为渲染或物理控制做任何特殊的事情,你不需要覆盖 Step()”。

于 2013-04-15T10:33:21.557 回答