Game Development Reference
In-Depth Information
textMon.appendText(xVel.toString());
textMon.appendText(",");
var yVel:Number=Math.round(velocity.y*worldScale);
textMon.appendText(yVel.toString());
}
}
world.DrawDebugData();
}
That's a lot of new code, anyway don't be scared as most of the code is used
just to update the text field.
Let's focus on the for loop first. The GetBodyList method gets the world
body list and returns the first body. You can get the next body with GetNext
and GetBodyList returns null if you are at the end of the list. So the three
parameters of the for loop can be read in the following way:
° var b:b2Body=world.GetBodyList() declares a b2Body
variable called b and assigns it the value of the first body in
the world body list.
° b is the exit condition, so we'll keep looping until b exists, that is,
b is not null , which means we are dealing with a body and we
aren't at the end of the list.
° b=b.GetNext()) gets the next body, it's very similar to an i++ in
a for loop, where i goes from 1 to n>1 .
Now inside the for loop we have a b variable representing the current body.
Let's see what to do with it. First, we need to know we are dealing with the
idol, so we are going to check its user data:
if (b.GetUserData()=="idol") { /* rest of the code */ }
Once we know we are working with the idol, let's get its position:
var position:b2Vec2=b.GetPosition();
The GetPosition method returns the body's origin position as a b2Vec2
object. If we want to know the position in pixels, we have to convert meters
to pixels as you are already used to doing it by now:
var xPos:Number=Math.round(position.x*worldScale);
var yPos:Number=Math.round(position.y*worldScale);
 
Search WWH ::




Custom Search