Simple 8-Way Movement

Here’s another old tutorial that I’ve bought over from the previous site, showing you how to create nice and simple 8 directional movement with rotation, handy for top down games such as RPG’s and the like.

I’ll probably carry this tutorial on at a later date to make a full game from it, until then, here’s the old one.

FrozenHaddock Tutorials: 8 Way Movement

In this tutorial, we’re going to create simple, 8-way, top-down movement. This is easy to implement and effective to use.

car
First, draw what you want to move.
You need to draw it facing upwards for the rotation to work.

carselected
Now select your character.

converttof8
Hit F8, and convert the car to a movie clip.

actionspanel
Open the actions panel for the car.
To do this, select the movie clip and hit F9.

ActionsOne
Now, this script creates the 8 way movement.
Unfortunately, this doesn’t make the character rotate.

longerscript
This needs to be added into the previous script.

rotatescript
And lines need to be added into the existing script.

So, the full script you have on there should be this-

  1. onClipEvent(load){
  2. speed=5;
  3. }
  4. onClipEvent(enterFrame){
  5. if(Key.isDown(Key.LEFT)&&Key.isDown(Key.UP)){
  6. this._x-=speed;
  7. this._y-=speed;
  8. this._rotation=315;
  9. }
  10. else if(Key.isDown(Key.LEFT)&&Key.isDown(Key.DOWN)){
  11. this._x-=speed;
  12. this._y+=speed;
  13. this._rotation=225;
  14. }
  15. else if(Key.isDown(Key.RIGHT)&&Key.isDown(Key.UP)){
  16. this._x+=speed;
  17. this._y-=speed;
  18. this._rotation=45;
  19. }
  20. else if(Key.isDown(Key.RIGHT)&&Key.isDown(Key.DOWN)){
  21. this._x+=speed;
  22. this._y+=speed;
  23. this._rotation=135;
  24. }
  25. else if(Key.isDown(Key.LEFT)){
  26. this._x-=speed;
  27. this._rotation=270;
  28. }
  29. else if(Key.isDown(Key.RIGHT)){
  30. this._x+=speed;
  31. this._rotation=90;
  32. }
  33. else if(Key.isDown(Key.UP)){
  34. this._y-=speed;
  35. this._rotation=0;
  36. }
  37. else if(Key.isDown(Key.DOWN)){
  38. this._y+=speed;
  39. this._rotation=180;
  40. }
  41. }

This should produce this

This can be used in many types of games, particularly RPG’s.

-->

21 Responses to “Simple 8-Way Movement”

  1. Wolfhunter Says:

    Im getting this error when i enter the script into flash cs3, while using a actionscript 2.0 document.

    ‘)’ expected
    For line 5:
    if(Key.isDown(Key.LEFT)&&Key.isDown(Key.UP)){

  2. Tazzydevil XIII Says:

    Hmm… I don’t see anywhere it would need a ), but it might just be the fact it’s 3am. I’ll check again when I’ve slept :)

  3. Tazzydevil XIII Says:

    Nope… no such luck… I have no idea why it’d return that error!

  4. lee Says:

    which version of flash are you using, because i only have MX, and the script is coming back with the following when i click test movie:

    Scene=Scene 1, Layer=Layer 1, Frame=1: Line 4: ‘{’ expected
    onClipEvent(enterFrame)}

    Scene=Scene 1, Layer=Layer 1, Frame=1: Line 5: Unexpected ‘}’ encountered
    if(Key.isDown(Key.LEFT))}

    Scene=Scene 1, Layer=Layer 1, Frame=1: Line 6: Statement must appear within on/onClipEvent handler
    this._x-=speed;

    Scene=Scene 1, Layer=Layer 1, Frame=1: Line 7: Unexpected ‘}’ encountered
    }

  5. lee Says:

    by the way, i haven’t added the rotation part in…

    (i didn’t want it to rotate…)

  6. lee Says:

    it’s okay. it is sorted. just a typeing error. cool script by the way. ive been looking for one for nearly a year.

  7. Tazzydevil XIII Says:

    Really? There should be plenty out there!

    Ah well, thanks :)

  8. Aaron Says:

    getting the same error wolfhunter got. if i take away the rotation it works…any ideas? also I’d like to use the awsd keys….can’t figure it out. I have this right now.
    onClipEvent(load){
    speed=5;
    }
    onClipEvent(enterFrame){
    if(Key.isDown(Key.”a”)){
    this._x-=speed;
    this._rotation=270;
    }
    else if(Key.isDown(Key.”d”)){
    this._x+=speed;
    this._rotation=90;
    }
    else if(Key.isDown(Key.”w”)){
    this._y-=speed;
    this._rotation=0;
    }
    else if(Key.isDown(Key.”s”)){
    this._y+=speed;
    this._rotation=180;
    }
    }

  9. Tazzydevil XIII Says:

    For aswd keys, you’d use Key.isDown(#) replacing # with the appropriate ASCII code.

    There’re a list of them here: http://blog.video-animation.com/2007/03/06/flash-8-ascii-keycodes/

  10. Aaron Says:

    you are the best.

  11. Aaron Says:

    yeah I keep getting

    ‘)’ expected
    For line 5:
    if(Key.isDown(Key.LEFT)&&Key.isDown(Key.UP)){

    frustrating.

  12. Tazzydevil XIII Says:

    Hmm, I’ve no idea why that’s happening.

    Let me investigate.

  13. vanax Says:

    I also got the ” ‘)’ expected For line 5 ” error. I fixed it by replacing the ” && ” with plain ” &&”. Probably its a syntax error do to the use of a ” ; ” within an if statement.

  14. Tazzydevil XIII Says:

    Ah, I see!

    Wordpress seems to take &’s and change them to the HTML friendly format…

    It should indeed be &&, thanks for spotting it!

  15. Man Says:

    I got
    1087: Syntax error: extra characters found after end of program.
    for line:
    onClipEvent(load){

    Why?

  16. Tazzydevil XIII Says:

    Hmmm, I’m not too sure…

    Are you definitely writing in AS2?

  17. ATLACH-NACHA Says:

    There are so many debugging errors in this tutorial.

  18. Tazzydevil XIII Says:

    What errors do you receive?

    Are you writing in AS2?

    Are you writing on a movie clip?

  19. AAron Says:

    The main problem I encountered, which seemed like the same one these others are encountering, is in the actionscript you have at the bottom of the screen. The errors are in lines 5, 10, 15, and 20. I found that by changing out the characters “&&” with “&&” I could get the file to work.

    For people experiencing this problem, here’s the code:

    onClipEvent(load){
    speed=5;
    }

    onClipEvent(enterFrame){
    if(Key.isDown(Key.LEFT)&&Key.isDown(Key.UP)){
    this._x-=speed;
    this._y-=speed;
    this._rotation=315;
    }
    else if(Key.isDown(Key.LEFT)&&Key.isDown(Key.DOWN)){
    this._x-=speed;
    this._y+=speed;
    this._rotation=225;
    }
    else if(Key.isDown(Key.RIGHT)&&Key.isDown(Key.UP)){
    this._x+=speed;
    this._y-=speed;
    this._rotation=45;
    }
    else if(Key.isDown(Key.RIGHT)&&Key.isDown(Key.DOWN)){
    this._x+=speed;
    this._y+=speed;
    this._rotation=135;
    }
    else if(Key.isDown(Key.LEFT)){
    this._x-=speed;
    this._rotation=270;
    }
    else if(Key.isDown(Key.RIGHT)){
    this._x+=speed;
    this._rotation=90;
    }
    else if(Key.isDown(Key.UP)){
    this._y-=speed;
    this._rotation=0;
    }
    else if(Key.isDown(Key.DOWN)){
    this._y+=speed;
    this._rotation=180;
    }
    }

  20. AAron Says:

    By all those ‘&’, I meant the little & amp part in your code above, but when I posted it the words changed lol

  21. Julian Says:

    Lmao i made a lil stickman and it looks like he’s floating in space
    and thanks for the new code aaron

    so all that needed to be changed is ‘&amp&amp’ into ‘&&’?

Leave a Reply