Add a sprite?

Hi,

Im create my box2d physic stuff, but how do I add a standard flash sprite to it, so that I dont have to use debug display?

Code:
var bodyDef:b2BodyDef = new b2BodyDef();
                bodyDef.type = b2Body.b2_dynamicBody;
                var boxDef:b2PolygonShape = new b2PolygonShape();
                fd = new b2FixtureDef();
                fd.shape = boxDef;
                fd.density = 1.0;
                // Override the default friction.
                fd.friction = 0.3;
                fd.restitution = 1;
                boxDef.SetAsBox((Math.random() * 5 + 10) / pixelsPerMeter, (Math.random() * 5 + 10) / pixelsPerMeter);
                bodyDef.position.Set((Math.random() * 400 + 120) / pixelsPerMeter, (Math.random() * 150 + 50) / pixelsPerMeter);
                bodyDef.angle = Math.random() * Math.PI;
                body = world.CreateBody(bodyDef);
                body.CreateFixture(fd);
body.getPosition() will return your x and y postion.
exact, in a b2Vec, so you can do this

var position:b2Vec = body.getPosition();
yourSprite.x = position.x;
yourSprite.y = position.y;
Okay great, so what happens when I want to move my sprite, do I have to move the box2d stuff as well? How do I do this?
Also your code came up with the following errors:

Type was not found or was not a compile-time constant: b2Vec.
Call to a possibly undefined method getPosition through a reference with static type Box2D.Dynamics:b2Body.
It's b2Vec2, not b2Vec.

Here's a code to move the sprites along with the bodies:
Code:
public function updateGraphics ():void {
         for (var b:b2Body = myWorld.GetBodyList(); b; b = b.GetNext()) {
            if (b.GetUserData() is Sprite){
               var sprite:Sprite = b.GetUserData() as Sprite;
               sprite.x = b.GetPosition().x/worldScale;
               sprite.y = b.GetPosition().y/worldScale;
               sprite.rotation = b.GetAngle()*180/Math.PI;
            }
         }
      }

myWorld is your world 😀 maybe you're calling it m_world.
I run updateGraphics() in the update function (where you run your world step).
worldScale is 1/30 (so you're actually multiplying by 30 - box2d uses meters not pixels, 1m = 30px).

The sprites will move if the bodies move.
So if you're bodies aren't static and your world has gravity, you should be able to see them move.

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