Search code examples
actionscript-3classoopflashdevelop

Not Displaying Nape Body


I had these two in the same Main class. Now there are Main.as and MainChar.as

When compiling doesn't show any errors but it isn't displaying the object either :c

It is the first time I am splitting code into different classes. I just figured what should be in the hero creation class and what should stay in Main.

package 
{
    import flash.display.Sprite;
    import flash.events.Event;
    import nape.geom.Vec2;
    import nape.phys.Body;
    import nape.phys.BodyList;
    import nape.space.Space;

    import MainChar;

    public class Main extends Sprite
    {
        public var gravity:Number = 600;
        public var space:Space = new Space(new Vec2(0, gravity));   
        public var hero:MainChar = new MainChar();

        public function Main():void
        {
            hero.createMainCharacter(stage.stageWidth/2, stage.stageHeight/2, 50, 50);
            addEventListener(Event.ENTER_FRAME, update);
        }

        private function update(e:Event):void 
        {
            space.step(1 / stage.frameRate, 10, 10);
            var bodies:BodyList = space.bodies;
            for (var i:int = 0; i < bodies.length; i++) 
            {
                var body:Body=bodies.at(i);
                if (body.userData.sprite != null) 
                {
                    body.userData.sprite.x = body.position.x;
                    body.userData.sprite.y = body.position.y;
                    body.userData.sprite.rotation=(body.rotation*180/Math.PI)%360;
                }
            }
        }
    }
}

Hero creator class:

package  
{
    import flash.display.Graphics;
    import flash.display.Sprite;
    import nape.geom.Vec2;
    import nape.phys.Body;
    import nape.phys.BodyType;
    import nape.shape.Polygon;
    import nape.space.Space;

    public class MainChar extends Sprite
    {
        public var space:Space = new Space(new Vec2(0, 600));

        public function MainChar():void 
        {
        } 

        public function createMainCharacter(x:Number, y:Number, width:Number, height:Number):void 
        {
            var mainChar:Body = new Body(BodyType.DYNAMIC);
            var mainCharShape:Polygon = new Polygon(Polygon.box(width, height));

            mainChar.shapes.add(mainCharShape);
            mainChar.position.setxy(x, y);
            mainChar.space = space;

            var mainCharSprite:Sprite = new Sprite();
                mainCharSprite.graphics.beginFill(0x000000);
                mainCharSprite.graphics.drawRect( -width/2, -height/2, width, height);
                mainCharSprite.graphics.endFill;
            addChild(mainCharSprite);

            mainChar.userData.sprite = mainCharSprite;
            addChild(mainChar.userData.sprite);
        }

    }
}

Solution

  • You need to add your hero as a child of your Main sprite EG:

      public function Main():void
      {
            hero.createMainCharacter(stage.stageWidth/2, stage.stageHeight/2, 50, 50);
            addEventListener(Event.ENTER_FRAME, update);
            addChild(hero);
      }