Game Development Stack Exchange is a question and answer site for professional and independent game developers. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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);
share|improve this question
+1 Love it when people can articulate there question, and have code samples, AND an example. Very refreshing :) – Adam Harte Aug 1 '11 at 12:11
up vote 3 down vote accepted

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);
share|improve this answer
Looks like you were converting from radians to degrees where you already had a value in degrees. – bummzack Jul 28 '11 at 8:28
This is the way to go... anyway you have to debug to see the numbers that runs under the hood – 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?

share|improve this answer
I checked my code again but I couldn't find any casting or int variable. – frbry Jul 28 '11 at 8:21

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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