Game Development Reference
In-Depth Information
Let's explain it line by line. The first two lines just convert mouse x and y
positions obtained with mouseX and mouseY respectively from values in
pixels to values in meters, according to the worldScale variable. This way
variables pX and pY will store world coordinates of the pixel we just clicked.
4.
Now it's time to see if there's a body in the point we clicked. The world's
QueryPoint method queries the world for all fixtures that contain a point,
and if a fixture contains the point clicked by the mouse, then we can say we
have clicked such a fixture.
Let's have a look at its arguments; first, a callback function we called
queryCallback , then the coordinates of the point we want to check
expressed as a b2Vec2 object, which represents translated mouse coordinates.
The queryCallback function is the core of this script and will take as
argument the fixture that contains the point, if any. As there could be more
than one fixture on the same point (think about overlapping static bodies),
you can make queryCallback return true if you want it to check for the
next fixture, or false to stop checking.
At the moment we assume there can be only one fixture under the mouse,
so I would write the queryCallBack function in the following way:
private function queryCallback(fixture:b2Fixture):Boolean {
trace(fixture);
return false;
}
At the moment we just want to generate some debug text in the output
window, so once you test the movie and click on a body, you'll see the
following text:
[object b2Fixture]
This means we have successfully executed the queryCallback function,
and we can determine the fixture that the player clicked on, if any.
5. Unfortunately, just knowing the fixture is not enough, as we also need to
know the body we are going to remove. To get a body starting from a fixture,
you can use the GetBody method, so you can make the following changes to
the queryCallBack function:
private function queryCallback(fixture:b2Fixture):Boolean {
var touchedBody:b2Body=fixture.GetBody();
trace(touchedBody);
return false;
}
 
Search WWH ::




Custom Search