[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 540
  • Last Modified:

4 way flash game movement

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.
0
robjay
Asked:
robjay
  • 2
  • 2
1 Solution
 
TanLiHaoCommented:
You didn't specify the Actionscript version you are using so I am just going to link you to a source I found

http://www.freeactionscript.com/2009/02/smooth-4-way-player-movement/

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.
0
 
robjayAuthor Commented:
Oh I am using Action Script 2.0...
And that movement isn't 4 way, you can go all 8 ways
0
 
TanLiHaoCommented:
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.


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

Featured Post

Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now