0

我使用 starling + as3 在我的移动应用程序上创建了舞台墙和一个盒子。

好的,现在当我测试应用程序时,盒子掉落但它与墙壁不匹配,好像有一个偏移量:

https://www.dropbox.com/s/hd4ehnfthh0ucfm/box.png

这是我创建盒子(墙壁和盒子)的方法。

似乎隐藏了一个偏移量,你怎么看?

public function createBox(x:Number, y:Number, width:Number, height:Number, rotation:Number = 0, bodyType:uint = 0):void {

            /// Vars used to create bodies
            var body:b2Body;
            var boxShape:b2PolygonShape;
            var circleShape:b2CircleShape;

               var fixtureDef:b2FixtureDef = new b2FixtureDef();
            fixtureDef.shape = boxShape;
            fixtureDef.friction = 0.3;
            // static bodies require zero density
            fixtureDef.density = 0;

             var quad:Quad;

                bodyDef = new b2BodyDef();               
                bodyDef.type = bodyType;
                bodyDef.position.x = x / WORLD_SCALE;
                bodyDef.position.y = y / WORLD_SCALE;

                // Box
                boxShape = new b2PolygonShape();
                boxShape.SetAsBox(width / WORLD_SCALE, height / WORLD_SCALE);
                fixtureDef.shape = boxShape;
                fixtureDef.density = 0;
                fixtureDef.friction = 0.5;
                fixtureDef.restitution = 0.2;

                // create the quads
                quad = new Quad(width, height, Math.random() * 0xFFFFFF);

                quad.pivotX = 0;
                quad.pivotY = 0;


                // this is the key line, we pass as a userData the starling.display.Quad
                bodyDef.userData = quad;

                //
                body = m_world.CreateBody(bodyDef);
                body.CreateFixture(fixtureDef);

                body.SetAngle(rotation * (Math.PI / 180));

                _clipPhysique.addChild(bodyDef.userData);   

        }
4

1 回答 1

0

SetAsBox 方法将一半宽度和一半高度作为其参数。我猜你的图形与你的 box2d 身体不匹配。因此,您要么需要将图形放大两倍,要么将 SetAsBox 参数乘以 0.5。身体枢轴也将位于它的中心,因此请根据其枢轴位置相应地偏移您的影片剪辑。

请注意,box2d 有一个调试器,它可以勾勒出你的身体,让你看看发生了什么。

于 2013-09-26T00:05:21.103 回答