I'm trying to create a simple topdown game, in which you control the player by WASD keys and use mouse to aim and shoot. So far, I have a player moving and firing, but I think there is something wrong with the shooting.

Accuracy gets lower as the mouse cursor move away from the player.

Please try the following SWF to see more clearly what I'm trying to say.


Here is a chunk of relevant code:

private function fireBullet(x:Number, y:Number, dFireAngle:Number):void {
            var b:Bullet = recycleBullet();
            var rFireAngle:Number;
            b.reset(x, y);
            b.angle = dFireAngle;
            rFireAngle = (dFireAngle * (Math.PI / 180));

            b.velocity.x = Math.cos(rFireAngle) * 385;
            b.velocity.y = Math.sin(rFireAngle) * 385;


if (FlxG.mouse.pressed()) {

                    var p:FlxPoint = new FlxPoint(FlxG.mouse.screenX, FlxG.mouse.screenY);
                    var angle:Number = FlxU.getAngle(new FlxPoint(player.x, player.y), p) - 90;
                    fireBullet(player.x, player.y, angle);
  • \$\begingroup\$ +1 Love it when people can articulate there question, and have code samples, AND an example. Very refreshing :) \$\endgroup\$ – Adam Harte Aug 1 '11 at 12:11

I used the following code instead of Flixel's getAngle and strangely, it works:

var dx:Number = x2 - x1;
var dy:Number = y2 - y1;
return Math.atan2(dy,dx);
  • \$\begingroup\$ Looks like you were converting from radians to degrees where you already had a value in degrees. \$\endgroup\$ – bummzack Jul 28 '11 at 8:28
  • \$\begingroup\$ This is the way to go... anyway you have to debug to see the numbers that runs under the hood \$\endgroup\$ – FxIII Jul 28 '11 at 9:15

The Flixel code just calls Math.atan2(Y,X) * 180 / Math.PI, which looks ok. Have you checked that you're not accidentally rounding floats down to ints at any point in those calculations?

  • \$\begingroup\$ I checked my code again but I couldn't find any casting or int variable. \$\endgroup\$ – frbry Jul 28 '11 at 8:21

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.