BodyList and acces to body variables

Hi everybody!

I'm new in the magic world of box2D and I got stucked with this problem: I'm using the following function to update the position of the userdata to the position of the body itself.

Code:
      public function Update(e:Event):void {
         the_world.Step(10, 1/30);
         for (var bb:b2Body = the_world.m_bodyList; bb; bb = bb.m_next) {
            if (bb.m_userData is Sprite) {
               bb.m_userData.life--;
               bb.m_userData.x = bb.GetPosition().x * 30;
               bb.m_userData.y = bb.GetPosition().y * 30;
               bb.m_userData.rotation = bb.GetAngle() * (180/Math.PI);
               if(bb.m_userData.life < 15){
                  bb.m_userData.width=2*bb.m_userData.width/3;
                  bb.m_userData.height=bb.m_userData.width;
                  bb.m_userData.y -= p2m(100);
               }
               if(bb.m_userData.life<=0){
                  bb.m_userData.x = -100;
                  bb.m_userData.y = -100;
                  the_world.DestroyBody(bb);
               }
            }
         }
      }


I have these questions:
1) How can I also access the real body's variables within the for loop? For example: let's assume that within this loop, if the variable life is smaller than 15, I want to scale down the userdata dimension (this I can do) AND the real object as well (so that collisions happen with the small object). How can I handle that?
2) How can I destroy the userdata (or how can I get the shape pointer, given a certain body so that I can invoke DestroyShape on it?). For example: let's assume that within this loop, if the variable life is smaller or equal to 0, I want to remove the body (this I can do) AND the userdata as well (I used the work-around to place it out of the stage, but I'm sure there's a better way).

Thank you for your attention!
You cannot scale bodies, only delete and recreate them. Other variables are accessed by the appropriate methods of the body.

By deleting a sprite, you mean removing it from the stage. This is done in the usual way:
sprite.parent.removeChild(sprite)
But userData is just a holder, Box2D has nothing to do with what you choose to store in it.
Hi and thank you for your reply!

So, to achieve the effect of scaling bodies I should just make a copy of them smaller, delete the original and then replace it with the copy. I tried to do that but the result is not what I want yet. It just deletes the circle I created and I can't understand why... I also didn't get how to get rid of the userdata shape. I tried to set it to null, but this raises an error... Here's the modified code, if someone can help me finding the error(s) I will really appreciate it 🙂 I'm using a variable "life" to see when the object should disappear. I want a smooth translation, so in the last 15 checks I try to reduce the size of the body...

Code:
public function Update(e:Event):void {
         the_world.Step(m_timeStep, m_iterations);
         for (var bb:b2Body = the_world.m_bodyList; bb; bb = bb.m_next) {
            if (bb.m_userData is Sprite) {
               if(bb.m_userData.life is Number){
                  trace("\n---\n"+bb+"\n"+bb.m_userData.life);
                  bb.m_userData.life--;
                  trace(bb.m_userData.life+"\n---\n");
               }
               bb.m_userData.x = bb.GetPosition().x * 30;
               bb.m_userData.y = bb.GetPosition().y * 30;
               bb.m_userData.rotation = bb.GetAngle() * (180/Math.PI);
               if(bb.m_userData.life < 15 && bb.m_userData.life > 0){
                  bb.m_userData.width=2*bb.m_userData.width/3;
                  bb.m_userData.height=bb.m_userData.width;
                  
                  //I create a new smaller body which is a copy of modified userdata parameters
                  var final_body:b2Body;
                  var the_body:b2BodyDef;
                  var circleDef:b2CircleDef;
                  the_body = new b2BodyDef();
                  the_body.position.Set(bb.m_userData.x, bb.m_userData.y);
                  circleDef = new b2CircleDef();
                  circleDef.radius=1;
                  //I move the pointer to the userdata
                  the_body.userData = bb.m_userData;
                  //I set the pointer of bb to null. But the_body still points at this memory location
                  bb.m_userData = null;
                  circleDef.density=1;
                  circleDef.friction=0.1;
                  circleDef.restitution=0.4;
                  final_body = the_world.CreateBody(the_body);
                  final_body.CreateShape(circleDef);
                  final_body.SetMassFromShapes();
                  addChild(the_body.userData);
                  the_world.DestroyBody(bb);
                  //I update the pointer so I can check if userData is 0 in the next selection
                  bb = final_body;
                  
               }
               
               if(bb.m_userData.life<=0){
                  bb.m_userData.x = -100;
                  bb.m_userData.y = -100;
                  the_world.DestroyBody(bb);
               }
            }
         }

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