Game Development Reference
In-Depth Information
brick(474,100,32,32);
brick(474,67,16,32);
brick(474,404,64,16);
brick(450,363,16,64);
brick(498,363,16,64);
brick(474,322,64,16);
var slingCanvas:Sprite=new Sprite();
slingCanvas.graphics.lineStyle(1,0xffffff);
slingCanvas.graphics.drawCircle(0,0,slingR);
addChild(slingCanvas);
slingCanvas.x=slingX;
slingCanvas.y=slingY;
theBird.graphics.lineStyle(1,0xfffffff);
theBird.graphics.beginFill(0xffffff);
theBird.graphics.drawCircle(0,0,15);
addChild(theBird);
theBird.x=slingX;
theBird.y=slingY;
theBird.addEventListener(MouseEvent.MOUSE_DOWN,birdClick);
addEventListener(Event.ENTER_FRAME,updateWorld);
}
We are just drawing some stuff, so there's nothing to say. Now, we need a function
to be executed when the player presses the mouse on the bird:
private function birdClick(e:MouseEvent):void {
stage.addEventListener(MouseEvent.MOUSE_MOVE,birdMove);
stage.addEventListener(MouseEvent.MOUSE_UP,birdRelease);
theBird.removeEventListener(MouseEvent.MOUSE_DOWN,birdClick);
}
The
birdMove
function will be used as long as the player moves the mouse keeping
the mouse button pressed, and will move the bird inside the sling area.
Notice how I am not using Box2D at this stage. There's no need to use it as there's no
physics involved when aiming with the bird, and one golden rule is
don't use physics
engines when they aren't needed
.
private function birdMove(e:MouseEvent):void {
theBird.x=mouseX;
theBird.y=mouseY;
var distanceX:Number=theBird.x-slingX;
var distanceY:Number=theBird.y-slingY;
if (distanceX*distanceX+distanceY*distanceY>slingR*slingR) {
var birdAngle:Number=Math.atan2(distanceY,distanceX);