4 way flash game movement

Posted on 2009-12-26
Last Modified: 2013-12-26
Hello Experts Exchange, I am working on a Flash game and was wondering how do I just get 4 way movement? Not 8 way movement.
Question by:robjay
    LVL 14

    Expert Comment

    You didn't specify the Actionscript version you are using so I am just going to link you to a source I found

    It should be easy enough for you to modify.

    In the future, when you ask questions, please specify the version of Actionscript you are using.

    Author Comment

    Oh I am using Action Script 2.0...
    And that movement isn't 4 way, you can go all 8 ways
    LVL 14

    Accepted Solution

    Hmm, the thing is, I know the author said it as 4 way movement. The general way people might interpret is 4 way is allowing for 4 keys to control, but the 4 keys cannot be pressed at the same time to create a diagonal effect.

    However, the author of the tutorial simply meant his tutorial as 4 way because only 4 keys are specified to control the movement. However, let's just stick to the 4 way you said and here's the method to do it.

    Given the object have an instance name object, then the code below shows how to do it. There are various way to do what you want, but the easiest is as shown below. Listeners are actually better way to do this, but since you could not figure out how to make a 4 way movement by yourself (not  meant to be an insult, but I have do hurt you I'm sorry but I have to let you know the reason for me showing you simpler code) then I will not show you the better ways to do it. It will only complicate things and make your life harder if you don't understand code. However, if this is a larger project, then you have to tell me the full details so I can write out the code for you. Please specify if you want classes though.

    var speed:Number = 5;

                       onEnterFrame = function():Void {
                           if(Key.isDown(Key.LEFT)) {
                               object._x -=speed;
                           } else if(Key.isDown(Key.RIGHT)) {
             object._x +=speed;
                           } else if(Key.isDown(Key.UP)){
                               object._y -=speed;
                           } else if(Key.isDown(Key.DOWN)){
                               object._y +=speed;

    This code have it's limitations like I said, it have a priority-based control system. When you hold on left, you can't press right, which effectively is not moving if you press left and right together. However, since this code prioritise the left key, then if you hold left and right together, the object will actually move left. That is perhaps something you might not want ( depending on your game).

    However, this eliminates the possibility of moving diagonally, because you are only allowed to hold one key at a time, otherwise the one above the other in code will just overwrite what you are pressing. If you are holding left and right, left takes priortiy.

    Left and up , left takes priority (no diagonal moving)
    Left and down, left takes priority (no diagonal moving)

    Right and up, right takes priority (no diagonal moving)
    And so on....

    I'm sorry for just giving you a random answer from the Internet but because you didn't specify the actionscript version it makes it hard for me to guess (I have at times written code for both AS2 and AS3 for other people but I can't just keep doing this).

    So I will apologise again for giving you an answer from just some search. You could have found your answer on searching though but since you are here, feel free to ask more since you paid for this (not that I am paid) but please tell me the scale of your project etc if you need better movement control.


    Author Comment

    Thank you TanLiHao, I now have made my basic walk movement. In the future I will say what action script I am using.

    Featured Post

    Do You Know the 4 Main Threat Actor Types?

    Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

    Join & Write a Comment

    What is RenderMan: RenderMan is a not any particular piece of software. RenderMan is an industry standard, defining set of rules that any rendering software should use, to be RenderMan-compliant. Pixar's RenderMan is a flagship implementation of …
    Artificial Intelligence comes in many forms, and for game developers, Path-Finding is an important ability for making an NPC (Non-Playable Character) maneuver through terrain.  A* is a particularly easy way to approach it.  I’ll start with the algor…
    The goal of the tutorial is to teach the user how to select which audio input to use. Once you have an audio input plugged into the laptop or computer, you will go into the audio input settings and choose which audio input you want to use.
    This Micro Tutorial will teach to how to utilize bit rate in Adobe Flash Media Live Encoder.

    734 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    23 Experts available now in Live!

    Get 1:1 Help Now