How to skin compound objects..

Issue is extremely easy I guess, but I'm beginner, and I've tried many times.

Compound object, like from this tutorial :

http://www.emanueleferonato.com/2008/12 ... ith-box2d/

I try to skin walls of the maze :
Code:
bodyDef = new b2BodyDef();
         bodyDef.userData=new wallBox();
         bodyDef.position.Set(1,3);
         body = m_world.CreateBody(bodyDef);
         for (var j=0; j<9; j++) {
            for (var i=0; i<9; i++) {
               if (mazeArr[j][i]==1)
               {
                  bodyDef.userData=new crate();
                  bodyDef.userData.width = 30;
                  bodyDef.userData.height = 30;
                  bodyDef.userData.name="wall";
                  addChild(bodyDef.userData);
                  boxDef = new b2PolygonDef();
                  boxDef.SetAsOrientedBox(0.5,0.5,new b2Vec2(i, j), 0);
                  boxDef.density=1;
                  boxDef.friction=0.5;
                  boxDef.restitution=0.2;
                  body.CreateShape(boxDef);
                  
               }
            }
         }
         body.SetMassFromShapes();


And it doesn't work...I guess it may be problem with Update function - in my code it looks exactly like in this tutorial..
Any suggestions appreciated 🙂
No suggestions? 😉 I'm fighting third day, and I guess it must be extremely easy..
By "it doesn't work" I suppose you're referring to the individual boxes not being displayed.
Your problems lies within the creation of the sprites (I'm assuming "crate" is a sprite/movieclip/asset whatsoever). First of all changing anything about the bodyDef after the body was created won't have any effect. What's more, you'd be overriding the body's userdata multiple times anyway. Just set the userData on the shapeDef instead (boxDef.userdata that would be). One more thing though: You'll have to set the x and y coordinates of the added sprites as well, since you're probably adding them to some sort of global stage when calling addChild.

With those changes your code should look somewhat like this:
Code:
bodyDef = new b2BodyDef();
bodyDef.userData=new wallBox();
bodyDef.position.Set(1,3);
body = m_world.CreateBody(bodyDef);
for (var j=0; j<9; j++) {
   for (var i=0; i<9; i++) {
      if (mazeArr[j][i]==1)
      {
         boxDef = new b2PolygonDef();
         boxDef.SetAsOrientedBox(0.5,0.5,new b2Vec2(i, j), 0);
         boxDef.density=1;
         boxDef.friction=0.5;
         boxDef.restitution=0.2;

         boxDef.userData = new crate();
         boxDef.userData.width = 30;
         boxDef.userData.height = 30;
         boxDef.userData.name = "wall";
         
         // Assuming your phys_scale is 30
         // Offset the x&y coords by -15, if the sprite's origin is in
         //  the upper left corner (instead of being centered)
         boxDef.userdata.x = i * 30 - 15;
         boxDef.userdata.y = j * 30 - 15;
         addChild(boxDef.userData);

         body.CreateShape(boxDef);
         
      }
   }
}
body.SetMassFromShapes();

Be warned however, if you ever move your body (and thereby also shift / tilt all the box-shapes), you'll also have to update the relevant sprites' coordinates. That'd be the point when you'd probably want to implement a separate class with update logic anyway. Considering you're setting up a static maze this won't be necessary though.

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