Game Development Reference
In-Depth Information
Selecting and destroying bodies with a
mouse click
We need to complete the Totem Destroyer prototype, so these concepts will be
applied to the script you made in Chapter 2 , Adding Bodies to the World . You should
have a totem ready to be destroyed with an idol on top of it.
1.
Before we see how to select and destroy bodies, we need to add a mouse
click listener, so we need to import a new class to handle mouse events in
our Main class:
import flash.display.Sprite;
import flash.events.Event;
import flash.events.MouseEvent;
import Box2D.Dynamics.*;
import Box2D.Collision.*;
import Box2D.Collision.Shapes.*;
import Box2D.Common.Math.*;
2.
Then we can add the mouse listener in the Main function:
public function Main() {
world=new b2World(new b2Vec2(0,5),true);
debugDraw();
brick(275,435,30,30);
brick(365,435,30,30);
brick(320,405,120,30);
brick(320,375,60,30);
brick(305,345,90,30);
brick(320,300,120,60);
idol(320,242);
floor();
addEventListener(Event.ENTER_FRAME,updateWorld);
stage.addEventListener(MouseEvent.CLICK,destroyBrick);
}
I am not explaining the previous code as it has nothing to do with Box2D,
and you should already know how to create a mouse listener.
3.
Things start to become interesting in the destroyBrick function, which is
called at every mouse click:
private function destroyBrick(e:MouseEvent):void {
var pX:Number=mouseX/worldScale;
var pY:Number=mouseY/worldScale;
world.QueryPoint(queryCallback,new b2Vec2(pX,pY));
}
 
Search WWH ::




Custom Search