Setting initial position for addChild properly.

I have an unusual problem.

I have placed 7 objects in flash onto the timeline, for a specified interval, they sit there and do nothing at all ... I have an AS3 layer that kicks in, and all 7 objects are removed from their original layer and replaced with 7 objects set up for physics with Box2D. The positions need to be exactly where the old static positions were ... so I just copied from the Flash Properties window and entered into the code where they are initially placed onto the scene ... as such.

The objects are all pulled from PhysicsData, into an array, then it's parsed and each body is added, and the x and y is defined depending on what the object is called... this all works fine.

Code: Select all

   if (value == "floor")
   {
      x = 0 / physicsData.ptm_ratio;
      y = 720 / physicsData.ptm_ratio;
   }
   
   body.SetPositionAndAngle(new b2Vec2(x, y), 0);
   
   // add the sprite to the scene
      addChild(bitmap);


I've tried changing the X to various values, using the pixel to metre ratio, not using it, applying different methods of initially placing the objects, ... all manner of coding, but no matter what, the first frame flashes up every single object all placed into the one single point, ... Then every frame after that the objects are all where they are meant to be, and behave correctly as intended. I've tried everything, but it's rediculous.

I can't get this one frame to bugger off!
You have 7 bodies with username Body and after each test value == "floor" You assign new coordinates XY. Probably lost link, then it is better to put all of the body in an array and -

var arr_body:Array = [];
var Body1:Body = .....
arr_body.push(Body1);
....

if (value == "floor")
{
x = 0 / physicsData.ptm_ratio;
y = 720 / physicsData.ptm_ratio;
arr_body[0].SetPositionAndAngle(new b2Vec2(x, y), 0);
}

well, very little source code to be able to talk about something specific.
So you say maybe push the body first then apply it's position? I'd have assumed that's why this is happening even though I push the body second... The sequence for the animation calculations happens second, after one step it assigns the correct values?

I'll post the entire thing...

Code: Select all

// Import standard flash Stuff.
import flash.display.Bitmap;
import flash.display.Sprite;
import flash.events.Event;
import flash.utils.getDefinitionByName;
 
// Import Box2D Stuff.
import Box2D.Dynamics.Joints.*;
import Box2D.Dynamics.*;
import Box2D.Collision.*;
import Box2D.Collision.Shapes.*;
import Box2D.Common.Math.*;
 
// import the sprite physics stuff.
import PhysicsData;

addEventListener(Event.ENTER_FRAME, update, false, 0, true);

// create an object containing the shape data.
var physicsData:PhysicsData = new PhysicsData();
 
// create the box2d world, set up gravity.
var world:b2World=new b2World(new b2Vec2(0,10.0),true);

// Fill an array with each item from PhysicsData.
var items:Array=["V", "E", "R", "T", "I", "G", "O", "floor"];

// Fill an array with each item from PhysicsData.
for each (var value:String in items) {
    // Grab the bitmap from the compiled resources
    var artClass:Class = getDefinitionByName(value) as Class;
   
   // Create all display object to add to the stage
    var bitmap:Bitmap = new Bitmap(new artClass(0, 0), "auto", true);
      
   // create a physics body from the physics data structure
    var body:b2Body=physicsData.createBody(value, world, b2Body.b2_dynamicBody, bitmap);
   
   // set to proper initial position
   if (value == "V")
   {
      x = 253.20 / physicsData.ptm_ratio;
      y = 289.75 / physicsData.ptm_ratio;
   }
   if (value == "E")
   {
      x = 376.20 / physicsData.ptm_ratio;
      y = 303.75 / physicsData.ptm_ratio;
   }
   if (value == "R")
   {
      x = 486.30 / physicsData.ptm_ratio;
      y = 283.75 / physicsData.ptm_ratio;
   }
   if (value == "T")
   {
      x = 578.40 / physicsData.ptm_ratio;
      y = 309.75 / physicsData.ptm_ratio;
   }
   if (value == "I")
   {
      x = 716.65 / physicsData.ptm_ratio;
      y = 289.75 / physicsData.ptm_ratio;
   }
   if (value == "G")
   {
      x = 753.10 / physicsData.ptm_ratio;
      y = 307.75 / physicsData.ptm_ratio;
   }
   if (value == "O")
   {
      x = 872.00 / physicsData.ptm_ratio;
      y = 287.75 / physicsData.ptm_ratio;
   }
   if (value == "floor")
   {
      x = 0 / physicsData.ptm_ratio;
      y = 720 / physicsData.ptm_ratio;
   }
   
   body.SetPositionAndAngle(new b2Vec2(x, y), 0);
   
   // add the sprite to the scene
      addChild(bitmap);
}

// Update
function update(e:Event):void {
    // step the world
    world.Step(1 / 25, 10, 10);
    world.ClearForces();
 
    // update the sprites from the physics data
    var Body:b2Body = world.GetBodyList();
    for (; Body; Body = Body.GetNext()) {
        if (Body.GetUserData() is Bitmap) {
 
            // get the user data value from the body
            var bitmap:Bitmap=Body.GetUserData() as Bitmap;
 
            // extract the position and set the sprite to it
            bitmap.x=Body.GetPosition().x * physicsData.ptm_ratio;
            bitmap.y=Body.GetPosition().y * physicsData.ptm_ratio;
         
            // set the rotation of the sprite
            bitmap.rotation = Body.GetAngle() * (180/Math.PI);
        }
    }
}
This is most likely due to the bitmap's position not being set when it renders the first frame. This code most likely runs in the first frame, which includes making the bitmaps and setting the physics positions, but the bitmaps' positions are not set during this first frame. The first time the update() method is called is in the second frame because you only just registered the update() method to the ENTER_FRAME event in that first frame. So either call the update() method or set the positions of the bitmaps (the bitmap position setting bit in the update method will do) in or after the for each loop.

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