Ygryggil wrote:
Code:
public function DrawDisplayObject(bb:b2Body):void
{
// get actor
var dispObj:DisplayObject = DisplayObject(bb.m_userData);
if(dispObj==null){return;}
// add actor if needed
if(!m_sprite.getChildByName(dispObj.name)){
m_sprite.addChild(dispObj);
}
// position actor
dispObj.x = bb.m_position.x * m_physScale;
dispObj.y = bb.m_position.y * m_physScale;
//dispObj.rotation = bb.m_rotation;
dispObj.rotation = bb.m_rotation * (180.0 / Math.PI);
}
{
// get actor
var dispObj:DisplayObject = DisplayObject(bb.m_userData);
if(dispObj==null){return;}
// add actor if needed
if(!m_sprite.getChildByName(dispObj.name)){
m_sprite.addChild(dispObj);
}
// position actor
dispObj.x = bb.m_position.x * m_physScale;
dispObj.y = bb.m_position.y * m_physScale;
//dispObj.rotation = bb.m_rotation;
dispObj.rotation = bb.m_rotation * (180.0 / Math.PI);
}
Where should the DrawDisplayObject function be called? I did the following:
Code:
body = m_world.CreateBody(bd);
body.m_userData = new Actor("char", myDisplayObject);
DrawDisplayObject(body);
body.m_userData = new Actor("char", myDisplayObject);
DrawDisplayObject(body);
My displayObject appears, but the line figure of the body also (apparently) appears, because moving the body, moves only the line figure. The DisplayObject seems to be fixed.
edit: I've solved my problem. For anyone else wondering about the same problem, I edited the end of the Update function:
Code:
// bodies
for (var bb:b2Body = m_world.m_bodyList; bb; bb = bb.m_next)
{
if(bb.m_userData != null)
{
DrawDisplayObject(bb);
}
else
{
for (var s:b2Shape = bb.GetShapeList(); s != null; s = s.GetNext())
{
DrawShape(s);
}
}
}
for (var bb:b2Body = m_world.m_bodyList; bb; bb = bb.m_next)
{
if(bb.m_userData != null)
{
DrawDisplayObject(bb);
}
else
{
for (var s:b2Shape = bb.GetShapeList(); s != null; s = s.GetNext())
{
DrawShape(s);
}
}
}