Diagonal Movement

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:


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 (

www.thestreakinggame.com

).

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

Thanks in advance

Steve

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.


//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.


//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.


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.

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

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: )

Personally I use comments to do the same thing :slight_smile:

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.

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

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


 
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.