Game Development Reference
In-Depth Information
var box2:b2Body=world.CreateBody(bodyDef);
box2.CreateFixture(fixtureDef);
addEventListener(Event.ENTER_FRAME,updateWorld);
stage.addEventListener(MouseEvent.MOUSE_DOWN,
createJoint);
}
3.
Don't worry about the createJoint callback function in the mouse down
listener. At the moment we aren't creating any joint, so we are just querying
the world in the same way we did when we learned how to destroy bodies.
private function createJoint(e:MouseEvent):void {
world.QueryPoint(queryCallback,mouseToWorld());
}
4.
Look at the mouseToWorld function now. As we are going to work a lot with
mouse coordinates, I created a little function to convert mouse position to a
b2Vec2 object with world coordinates.
private function mouseToWorld():b2Vec2 {
return new b2Vec2(mouseX/worldScale,mouseY/worldScale);
}
5.
And the queryCallback function just checks if the clicked body is a dynamic
body with the GetType method. You are going to drag bodies, so it's obvious
you want to check if a body is dynamic. In this step, let's just write some text
in the output window:
private function queryCallback(fixture:b2Fixture):Boolean {
var touchedBody:b2Body=fixture.GetBody();
if (touchedBody.GetType()==b2Body.b2_dynamicBody) {
trace("will create joint here");
}
return false;
}
6.
In the end, the debug draw function:
private function debugDraw():void {
var debugDraw:b2DebugDraw=new b2DebugDraw();
var debugSprite:Sprite=new Sprite();
addChild(debugSprite);
debugDraw.SetSprite(debugSprite);
debugDraw.SetDrawScale(worldScale);
debugDraw.SetFlags(b2DebugDraw.e_shapeBit);
debugDraw.SetFillAlpha(0.5);
world.SetDebugDraw(debugDraw);
}
 
Search WWH ::




Custom Search