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 want my top-down shooter character to move towards the mouse when pressing the up arrow and strafe relative at right angles to it when pressing the left and right arrow keys.

I can't figure out how to do this.

Here is my code (The character is character_mc):

//Event Listners
crosshair_mc.mouseEnabled = false;
crosshair_mc.addEventListener(Event.ENTER_FRAME, fl_CustomMouseCursor);

function fl_CustomMouseCursor(event:Event)
    crosshair_mc.x = stage.mouseX;
    crosshair_mc.y = stage.mouseY;

stage.addEventListener(KeyboardEvent.KEY_DOWN, fl_KeyboardDownHandler);
function facecursor(event):void
    character_mc.rotation = (180 * Math.atan2(mouseY - character_mc.y,mouseX - character_mc.x))/Math.PI + 90;


function fl_KeyboardDownHandler(event:KeyboardEvent):void
    trace("Key Code Pressed: " + event.keyCode);
    if (event.keyCode == 38)
        character_mc.y = character_mc.y - 5;
    if (event.keyCode == 40)
        character_mc.y = character_mc.y + 5;
        if (event.keyCode == 39)
        character_mc.x = character_mc.x + 5;
        if (event.keyCode == 37)
        character_mc.x = character_mc.x - 5;


I'm new to actionscript, so example code would be helpful.

share|improve this question
up vote 0 down vote accepted

The direction to the cursor is given by cursor - character position (in screenspace). This is the direction you want to move towads when moving 'forward'.

So, you would add this direction to your characters position to move towards the mouse cursor. You would then move your character like this:

//assuming scaleFactor is a variable with value < 1.0
character_mc.x = character_mc.x + scaleFactor * (crosshair_mc.x - character_mc.x);
character_mc.y = character_mc.y + scaleFactor * (crosshair_mc.y - character_mc.y);

.. if the 'move forward' key is pressed

But, using this vector directly will move/warp your character directly to the mouse cursor, so you might want to scale it down a bit. Ofc, if you where using vectors, this would be easier (e.g. you could multiply the normalized direction by a speed instead of using a scaleFactor).

To move to either the side, you would need to move to a direction -90° or 90° from the forward direction.

Edit: The distance between the character and mouse cursor influences the above formula, thus the character moves faster the further the mouse is away.

This can be changed to be a constant speed by either calculating a constant direction from the characters rotation (using trigonometric functions) or by normalizing the direction vector. In both cases, the result is multiplied by the characters speed and added to her position.

For more information about vector algebra, see e.g. here:

share|improve this answer
can you explain how to move it at a fixed speed more thoroughly please – user2130844 Mar 9 '13 at 12:27
I extended to the answer. – sarahm Mar 9 '13 at 18:37
thanks so much for your help i figured it out – user2130844 Mar 10 '13 at 1:20

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.