# Diagonal Movement

**URL:** https://forum.kirupa.com/t/diagonal-movement/186458
**Category:** programming
**Created:** [April 21, 2006, 11:00am UTC](https://forum.kirupa.com/t/diagonal-movement/186458 "2006-04-21T11:00:52Z")
**Posts on this page:** 9
**Page:** 1

<div class="post-metadata">

### Author: ![stevesavage](https://avatars.discourse-cdn.com/v4/letter/s/edb3f5/32.png) [@stevesavage](https://forum.kirupa.com/u/stevesavage)
#### Post date: [April 21, 2006, 11:00am UTC](https://forum.kirupa.com/t/diagonal-movement/186458/1 "2006-04-21T11:00:52Z")

</div>

Hi,

I’m putting together a small game for a friend and I’m having a few problems with some movement code.

Basically, I have a character on the screen, who needs to be able to move in 8 directions (up, down, left, right, up and left, up and right, down and left, down and right)… simple so far.

I also need the character to animate when moving in one of the 8 directions.

The character movieclip is named “alien”. The is the code I have so far:

```auto

onClipEvent (enterFrame) {
if (Key.isDown(40)) {
_y+= _root.walkSpeed;
this.gotoAndStop(5);
}
if (Key.isDown(38)) {
_y-=_root.walkSpeed;
this.gotoAndStop(10);
}
if (Key.isDown(39)) {
_x+=_root.walkSpeed;
this.gotoAndStop(15);
}
if (Key.isDown(37)) {
_x-=_root.walkSpeed;
this.gotoAndStop(20);
}
if (Key.isDown(37)&&(Key.isDown(38))) {
_x-=_root.walkSpeed/2;
_y-=_root.walkSpeed/2;
this.gotoAndStop(25);
}
}
onClipEvent (enterFrame) {
if (Key.isDown(39)&&(Key.isDown(38))) {
_x+=(_root.walkSpeed/2);
_y-=_root.walkSpeed/2;
this.gotoAndStop(30);
}
}
onClipEvent (enterFrame) {
if (Key.isDown(39)&&(Key.isDown(40))) {
_x+=_root.walkSpeed/2;
_y+=_root.walkSpeed/2;
this.gotoAndStop(35);
}
if (Key.isDown(40)&&(Key.isDown(37))) {
_x-=_root.walkSpeed/2;
_y+=_root.walkSpeed/2;
this.gotoAndStop(40);
}

```

But it doesn’t work…

Can anyone help me to simplify or simply rewrite this code?

I saw an example recently on another forum for a game called The Streaking Game (

```auto
www.thestreakinggame.com

```

).

This is exactly the movement I’m trying to recreate.

Thanks in advance

Steve

---

<div class="post-metadata">

### Author: ![Lord\_Rahl](https://avatars.discourse-cdn.com/v4/letter/l/a87d85/32.png) [@Lord\_Rahl](https://forum.kirupa.com/u/Lord_Rahl)
#### Post date: [April 21, 2006, 5:20pm UTC](https://forum.kirupa.com/t/diagonal-movement/186458/2 "2006-04-21T17:20:50Z")

</div>

Okay two things.  
1.) You don’t define “walkSpeed” anywhere. Now the way you have it set up its reading the main timeline due to the “\_root.” command. So you have to put the “walkSpeed” on the main timeline.

```auto

//for anyone who doesn't know how to define things
walkSpeed = 5;

```

Or you can take out the “\_root.” command and be able to define it directly in the movement script. Like the following.

```auto

//this is only the beggining of your code
onClipEvent (enterFrame) {
 walkSpeed = 5;
 if (Key.isDown(40)) {
  _y+= walkSpeed;
  this.gotoAndStop(5);
 }
 if (Key.isDown(38)) {
  _y-=walkSpeed;
  this.gotoAndStop(10);
 }

```

2.) Your missing a bracket at the end.

```auto

onClipEvent (enterFrame) {
 if (Key.isDown(40)) {
  _y+= _root.walkSpeed;
  this.gotoAndStop(5);
 }
 if (Key.isDown(38)) {
  _y-=_root.walkSpeed;
  this.gotoAndStop(10);
 }
 if (Key.isDown(39)) {
  _x+=_root.walkSpeed;
  this.gotoAndStop(15);
 }
 if (Key.isDown(37)) {
  _x-=_root.walkSpeed;
  this.gotoAndStop(20);
 }
 if (Key.isDown(37)&&(Key.isDown(38))) {
  _x-=_root.walkSpeed/2;
  _y-=_root.walkSpeed/2;
  this.gotoAndStop(25);
 }
}
onClipEvent (enterFrame) {
 if (Key.isDown(39)&&(Key.isDown(38))) {
  _x+=(_root.walkSpeed/2);
  _y-=_root.walkSpeed/2;
  this.gotoAndStop(30);
 }
}
onClipEvent (enterFrame) {
 if (Key.isDown(39)&&(Key.isDown(40))) {
  _x+=_root.walkSpeed/2;
  _y+=_root.walkSpeed/2;
  this.gotoAndStop(35);
 }
 if (Key.isDown(40)&&(Key.isDown(37))) {
  _x-=_root.walkSpeed/2;
  _y+=_root.walkSpeed/2;
  this.gotoAndStop(40);
 **}**
}

```

Above is your code with the bracket you missed shown in bold.

---

<div class="post-metadata">

### Author: ![DangerousDan](https://avatars.discourse-cdn.com/v4/letter/d/ad7895/32.png) [@DangerousDan](https://forum.kirupa.com/u/DangerousDan)
#### Post date: [April 21, 2006, 6:10pm UTC](https://forum.kirupa.com/t/diagonal-movement/186458/3 "2006-04-21T18:10:25Z")

</div>

Whats up with the multiple enterFrame()?  
I’m pretty sure you only need one, correct me if I’m wrongI:-)

---

<div class="post-metadata">

### Author: ![Lord\_Rahl](https://avatars.discourse-cdn.com/v4/letter/l/a87d85/32.png) [@Lord\_Rahl](https://forum.kirupa.com/u/Lord_Rahl)
#### Post date: [April 21, 2006, 7:53pm UTC](https://forum.kirupa.com/t/diagonal-movement/186458/4 "2006-04-21T19:53:59Z")

</div>

Well some people use multiple functions as a way of seperating things for organization. I use many “onClipEvent(enterFrame){” myself so that I can use that as a way of scrolling thru and knowing when to stop. Its always good to do what makes things more simple for you to understand. (Did that last sentence make any sense? :huh: )

---

<div class="post-metadata">

### Author: ![Nich](https://avatars.discourse-cdn.com/v4/letter/n/e79b87/32.png) [@Nich](https://forum.kirupa.com/u/Nich)
#### Post date: [April 22, 2006, 1:54am UTC](https://forum.kirupa.com/t/diagonal-movement/186458/5 "2006-04-22T01:54:47Z")

</div>

Personally I use comments to do the same thing 🙂

---

<div class="post-metadata">

### Author: ![nathan99](https://avatars.discourse-cdn.com/v4/letter/n/96bed5/32.png) [@nathan99](https://forum.kirupa.com/u/nathan99)
#### Post date: [April 22, 2006, 2:22am UTC](https://forum.kirupa.com/t/diagonal-movement/186458/6 "2006-04-22T02:22:06Z")

</div>

yeah, i wouldn’t use multiple enterFrames to save PCjust separeted them with  
///////////  
///

or something

---

<div class="post-metadata">

### Author: ![Lord\_Rahl](https://avatars.discourse-cdn.com/v4/letter/l/a87d85/32.png) [@Lord\_Rahl](https://forum.kirupa.com/u/Lord_Rahl)
#### Post date: [April 22, 2006, 2:56am UTC](https://forum.kirupa.com/t/diagonal-movement/186458/7 "2006-04-22T02:56:28Z")

</div>

> [@nathan99](#):
>
> yeah, i wouldn’t use multiple enterFrames to save PCjust separeted them with  
> ///////////  
> ///
> 
> or something

Thats actually what i’m starting to do now. You’ll see alot of  
//Funny script section  
//AI section  
in my coding as of late.

---

<div class="post-metadata">

### Author: ![joran420](https://yyz1.discourse-cdn.com/flex011/user_avatar/forum.kirupa.com/joran420/32/2329_2.png) [@joran420](https://forum.kirupa.com/u/joran420)
#### Post date: [April 22, 2006, 5:47am UTC](https://forum.kirupa.com/t/diagonal-movement/186458/8 "2006-04-22T05:47:12Z")

</div>

maybe try putting the if statement with the && above the if statements without

as keyDown Right will evaluate as true even if you have key up and right down  
so check multipresses before single presses

---

<div class="post-metadata">

### Author: ![Spartan\_1117](https://avatars.discourse-cdn.com/v4/letter/s/7ea924/32.png) [@Spartan\_1117](https://forum.kirupa.com/u/Spartan_1117)
#### Post date: [April 22, 2006, 2:16pm UTC](https://forum.kirupa.com/t/diagonal-movement/186458/9 "2006-04-22T14:16:24Z")

</div>

I recently had this problem also, this is what works for me:

```auto

 
onClipEvent (load) {
 speed = 0;
 diagonalSpeed = speed/Math.SQRT2;
}
onClipEvent (enterFrame) {
 if (Key.isDown(Key.RIGHT)) {
  if (Key.isDown(Key.UP)) {
   _x -= -diagonalSpeed;
   _y -= diagonalSpeed;
   gotoAndStop(16);
///This means that if right and up are presed, goto the animation on frame 
///16, the animation should be walking up, and right. You might have to 
/// change the frame number
  } else if (Key.isDown(Key.DOWN)) {
   _x -= -diagonalSpeed;
   _y -= -diagonalSpeed;
   gotoAndStop(14);
///The same here, except for down
  } else {
   _x -= -speed;
   gotoAndStop(8);
///This means if only right is pressed, goto the frame with right animation
  }
 } else if (Key.isDown(Key.LEFT)) {
  if (Key.isDown(Key.UP)) {
   _x -= diagonalSpeed;
   _y -= diagonalSpeed;
   gotoAndStop(12);
  } else if (Key.isDown(Key.DOWN)) {
   _x -= diagonalSpeed;
   _y -= -diagonalSpeed;
   gotoAndStop(10);
  } else {
   _x -= speed;
   gotoAndStop(6);
  }
 } else {
  if (Key.isDown(Key.UP)) {
   _y -= speed;
   gotoAndStop(4);
  } else if (Key.isDown(Key.DOWN)) {
   _y -= -speed;
   gotoAndStop(2);
  } else {
   if (_currentframe == 16) {
    gotoAndStop(15);
///All of these mean, that if you are walking in one direction, and you 
/// release the key, got to the frame of him facing that direction but not 
/// walkin, this makes him seem more realistic
   }
   if (_currentframe == 14) {
    gotoAndStop(13);
   }
   if (_currentframe == 12) {
    gotoAndStop(11);
   }
   if (_currentframe == 10) {
    gotoAndStop(9);
   }
   if (_currentframe == 8) {
    gotoAndStop(7);
   }
   if (_currentframe == 6) {
    gotoAndStop(5);
   }
   if (_currentframe == 4) {
    gotoAndStop(3);
   }
   if (_currentframe == 2) {
    gotoAndStop(1);
   }
  }
 }
}

```

Ok, that works for MY character. What you will have to do is change the frame numbers and the keys. Hope this works for you, if not post what your having problems with and I’ll see what I can do.
