cant add .UserData Sprite to displayList

Greetings,

I started exploring Box2D some time ago, and find it absolutely genius, anyhow i have a problem with displaying custom image in b2Body (in this case hero).

Game.as
Code:
package
{

   import Box2D.Collision.b2AABB;
   import Box2D.Common.Math.b2Vec2;
   import Box2D.Dynamics.b2DebugDraw;
   import Box2D.Dynamics.b2World;
   
   import Caracters.Hero;
   
   import flash.display.MovieClip;
   import flash.display.Sprite;
   import flash.events.Event;
   
   import shapes.StaticBox;
   
   
   
   [SWF(width='500', height='400', backgroundColor='0xFF9900' )]
   
   public class Game extends MovieClip
   {
      //swf
      private var swfSirina:Number;
      private var swfVisina:Number;
      
      
      //world
      private var worldHolder:Sprite = new Sprite();
      private var world:b2World;
      private var worldAABB:b2AABB = new b2AABB;
      private var gravity:b2Vec2 = new b2Vec2(0,10);
      private var doSleep:Boolean = false;
      private var timeStep:Number = 1/60;
      private var iterations:int = 10;
      
      private var hero:Hero;
      private var staticBox;
      private var debugDraw:b2DebugDraw;
      
      
      public function Game()
      {
         addEventListener(Event.ADDED_TO_STAGE, onStage, false, 0, true);
      }
      private function onStage(e:Event):void
      {
         removeEventListener(Event.ADDED_TO_STAGE, onStage);
         init();
      }
      
      private function init():void
      {
         createWorld();
         hero = new Hero(100,20,world)
                         
                         //======================================
         /* if uncomented sprite in userData is added to stage
         var s:Sprite = new Sprite();
         addChild(s)
         s.addChild(hero);
         */
                         //======================================
   
         createDebugDraw();
         addEventListener(Event.ENTER_FRAME, onFrame, false, 0, true);

      }
      
      private function createWorld():void
      {
         worldAABB.lowerBound.Set(-100,-100);
         worldAABB.upperBound.Set(100,100);
         world = new b2World(worldAABB, gravity, doSleep);
         
         
         //zidovi 10px debljine
         var groundWall:StaticBox = new StaticBox(0,stage.stageHeight-10,stage.stageWidth,10,"platform", world)
         var cellingWall:StaticBox = new StaticBox(0,-10,stage.stageWidth,10,"wall",world);
         var leftWall:StaticBox = new StaticBox(-10,0,10,stage.stageHeight,"wall", world);
         var rightWall:StaticBox = new StaticBox(stage.stageWidth,0,10,stage.stageHeight,"wall",world)
      }

      private function createDebugDraw():void
      {
         debugDraw = new b2DebugDraw();
         debugDraw.m_drawScale = Convert.M2PRATIO;
         debugDraw.m_fillAlpha = 0.3
         debugDraw.m_drawFlags = b2DebugDraw.e_shapeBit;
         debugDraw.m_sprite = this;
         world.SetDebugDraw(debugDraw);
      }
      
      private function onFrame(e:Event):void
      {
         world.Step(timeStep,iterations)
      }   
   }
}



And Hero.as class

Code:
package Caracters
{
   import Box2D.Collision.Shapes.b2PolygonDef;
   import Box2D.Common.Math.b2Vec2;
   import Box2D.Dynamics.b2Body;
   import Box2D.Dynamics.b2BodyDef;
   import Box2D.Dynamics.b2World;
   
   import flash.display.Sprite;
   import flash.events.Event;
   import flash.events.KeyboardEvent;
   import flash.ui.Keyboard;
   
   

   public class Hero extends Sprite
   {
      [Embed(source="../assets/avatar.jpg")]
      public var HeroG:Class
      
      // HERO VARS
      private var hero:b2Body;
      private var heroHeight:Number = 20
      private var heroWidth:Number = 10
      private var feetSensorWidth:Number = 7;
      private var feetSensorHeight:Number = 2;
      private var handsSensorWidth:Number = 12;
      private var handsSensorHeight:Number = 10;
      
      //KEYBOARD VARS   
      private var up:Boolean = false
      private var down:Boolean = false
      private var left:Boolean = false
      private var right:Boolean = false
      private var space:Boolean = false;
      
      private var boxFace:BoxFace;
      

      public function Hero(startX:Number, startY:Number, mainWorld:b2World)
      {
         
         // BODY CREATION
         var bodyDef:b2BodyDef = new b2BodyDef();
         bodyDef.position.Set(Convert.toPosition(startX)+Convert.toDimension(heroWidth), Convert.toPosition(startY)+Convert.toDimension(heroHeight));
         bodyDef.allowSleep = false;
         bodyDef.fixedRotation = true;
         bodyDef.isBullet = true;
         
         hero = mainWorld.CreateBody(bodyDef);
         
         // GRAPHICS CREATION -- i cant get this spirite to show on stage...
         var heroG:Sprite = new Sprite();
         heroG.addChild(new HeroG)
         bodyDef.userData = heroG;
         bodyDef.userData.width = heroWidth
         bodyDef.userData.height = heroHeight
         addChild(bodyDef.userData)
         
         
         
         //SHAPE
         var shapeDef:b2PolygonDef = new b2PolygonDef();
         shapeDef.SetAsBox(Convert.toDimension(heroWidth),Convert.toDimension(heroHeight));
         shapeDef.density = 1;
         shapeDef.friction = 1;
         shapeDef.restitution = 0.001;
         
         hero.CreateShape(shapeDef);
         hero.SetMassFromShapes();
         
         // SENSORS CREATION
         var heroFeetSensor:b2PolygonDef = new b2PolygonDef();
         heroFeetSensor.SetAsOrientedBox(Convert.toDimension(feetSensorWidth), Convert.toDimension(feetSensorHeight), new b2Vec2(0, Convert.toPosition(heroHeight/2)),0);
         heroFeetSensor.isSensor = true;
         heroFeetSensor.userData = "heroFeet";
         
         hero.CreateShape(heroFeetSensor);
         hero.SetMassFromShapes();
         
         var heroHandsSensor:b2PolygonDef = new b2PolygonDef();
         heroHandsSensor.SetAsOrientedBox(Convert.toDimension(handsSensorWidth),Convert.toDimension(handsSensorHeight),new b2Vec2(0,0),0)
         heroHandsSensor.isSensor = true;
         heroHandsSensor.userData = "heroHands"
         
         hero.CreateShape(heroHandsSensor);
         hero.SetMassFromShapes();
         

         //EVENTS CREATION -- stage event never fires off
         //addEventListener(Event.ADDED_TO_STAGE, onStage, false, 0, true);
      }


Everything is displayed fine if i uncomment part i marked in Game class so measurement and everything else works fine.
Problem is that i do not understand how to display custom graphics on body's and why isnt user data at hero body displayed, unless i wrap it into sprite at main class ?

Also if that is case (that every Box2D body needs to be added to sprite), what is best practice in doing so.

Its interesting that if i move Hero logic from separate class into Game class, everything works fine without adding adidtional sprites.

Thanks in advance.
currently you are adding your UserData to the Hero class.
since Hero extends Sprite this is fine.

but hero (your instance of the Hero Class) is not on the stage.
so, assuming that your Game class is the stage or is on the stage... then you must add hero to display on your Game class.

Box2D classes are built to handle the calculations, and are ported to many different languages. they leave the display of art up to you. The HelloWorld example is a good place to start.

keep in mind that UserData is just a data variable for you to store things in, anything... so its not made special in any way for graphics.

also, currently your graphics wouldn't move anywhere, because you are not applying the position and rotation of the bodies to your graphics. see the HelloWorld example and look at the update function.


as for "best practices" a popular topic on the threads has been Actor classes. give that a search.

best of luck to you.
Thank you for your answer, ill be sure to check Actor topics.

Original thread: https://box2d.org/forum/viewtopic.php?t=3892