AS3 Display list and box2d

2019-09-08 23:56发布

问题:

I have a function called "brick" inside a subclass called createBrick that extends from the sprite class, also I have a movie clip created in the library exported for runtime sharing called "Brick". For some odd reason when I run the code the brick is not showing up on the stage even though it does get created when I debug the code.

here is the function

     public  class createBrick extends Sprite {

        public function createBrick(_main:Main) {
                    main = _main; 
                    var go:Brick = new Brick();
                    addChild(go);
                    trace(go.x);
                    brick(475, 235, 30, 30);
        }

    private function brick(pX: int, pY: int, w: Number, h: Number): void {

                var bric:Brick = new Brick();
                addChild(bric);
                bric.x = pX;
                bric.y = pY;
                bric.width = w;
                bric.height = h;

                var polygonShape: b2PolygonShape = new b2PolygonShape();
                var polygonFixture: b2FixtureDef = new b2FixtureDef();
                polygonShape.SetAsBox(w / 2 / worldScale, h / 2 / worldScale);
                polygonFixture.shape = polygonShape;
                polygonFixture.density = .2;
                polygonFixture.restitution = 0.9;
                polygonFixture.friction = 0.9;
                var brickbodyDef: b2BodyDef = new b2BodyDef();
                brickbodyDef.type=b2Body.b2_dynamicBody;
                brickbodyDef.userData = bric;
                brickbodyDef.position.Set(bric.x / worldScale, bric.y / worldScale);
                var theBrick: b2Body = world.CreateBody(brickbodyDef);
                theBrick.CreateFixture(polygonFixture);
                }
        }

The same function works perfectly if I have all the code with in the main document class and I do see a brick on the stage. Any clues Am I not referencing it properly?

回答1:

I figured it out, just had to pass the stage reference from main document class for the sprite to show up.