8-Way Movement

From Game Editor

Revision as of 08:23, 9 July 2009 by Skydereign (Talk | contribs)
(diff) ← Older revision | Current revision (diff) | Newer revision → (diff)
Jump to: navigation, search

Platformers and many other games do not need as complex a system as this example. Games that would implement 8 directional movement would be games such as simple space shooters, or similar, which in essence are overhead games. Even then, these games usually do not require regulation of animation in this fashion, as they would use Rotation. This method would more likely be used for overhead games that involve walking.


This example implements the all possible for the keydowns, and animindex as a state system for any keyup events.


Player -> KeyDown event -> Up (Enabled) -> Script Editor

char * key = GetKeyState();
if (key[KEY_DOWN]==0 && key[KEY_LEFT]==0 && key[KEY_RIGHT]==0)
{
  ChangeAnimation("Event Actor", "run_north", NO_CHANGE);
}
else if (key[KEY_DOWN]==1 && key[KEY_LEFT]==0 && key[KEY_RIGHT]==0)
{
  ChangeAnimation("Event Actor", "stand_south", NO_CHANGE);
}
else if (key[KEY_DOWN]==0 && key[KEY_LEFT]==1 && key[KEY_RIGHT]==0)
{
  ChangeAnimation("Event Actor", "run_northwest", NO_CHANGE);
}
else if (key[KEY_DOWN]==0 && key[KEY_LEFT]==0 && key[KEY_RIGHT]==1)
{
  ChangeAnimation("Event Actor", "run_northeast", NO_CHANGE);
}
else if (key[KEY_DOWN]==0 && key[KEY_LEFT]==1 && key[KEY_RIGHT]==1)
{
  ChangeAnimation("Event Actor", "run_north", NO_CHANGE);
}
else if (key[KEY_DOWN]==1 && key[KEY_LEFT]==1 && key[KEY_RIGHT]==0)
{
  ChangeAnimation("Event Actor", "run_west", NO_CHANGE);
}
else if (key[KEY_DOWN]==1 && key[KEY_LEFT]==0 && key[KEY_RIGHT]==1)
{
  ChangeAnimation("Event Actor", "run_east", NO_CHANGE);
}


Player -> KeyDown event -> Down (Enabled) -> Script Editor

char * key = GetKeyState();
if (key[KEY_UP]==0 && key[KEY_LEFT]==0 && key[KEY_RIGHT]==0)
{
  ChangeAnimation("Event Actor", "run_south", NO_CHANGE);
}
else if (key[KEY_UP]==1 && key[KEY_LEFT]==0 && key[KEY_RIGHT]==0)
{
  ChangeAnimation("Event Actor", "stand_north", NO_CHANGE);
}
else if (key[KEY_UP]==0 && key[KEY_LEFT]==1 && key[KEY_RIGHT]==0)
{
  ChangeAnimation("Event Actor", "run_southwest", NO_CHANGE);
}
else if (key[KEY_UP]==0 && key[KEY_LEFT]==0 && key[KEY_RIGHT]==1)
{
  ChangeAnimation("Event Actor", "run_southeast", NO_CHANGE);
}
else if (key[KEY_UP]==0 && key[KEY_LEFT]==1 && key[KEY_RIGHT]==1)
{
  ChangeAnimation("Event Actor", "run_south", NO_CHANGE);
}
else if (key[KEY_UP]==1 && key[KEY_LEFT]==1 && key[KEY_RIGHT]==0)
{
  ChangeAnimation("Event Actor", "run_west", NO_CHANGE);
}
else if (key[KEY_UP]==1 && key[KEY_LEFT]==0 && key[KEY_RIGHT]==1)
{
  ChangeAnimation("Event Actor", "run_east", NO_CHANGE);
}


Player -> KeyDown event -> Right (Enabled) -> Script Editor

char* key = GetKeyState();
if (key[KEY_UP]==0 && key[KEY_LEFT]==0 && key[KEY_DOWN]==0)
{
  ChangeAnimation("Event Actor", "run_east", NO_CHANGE);
}
else if (key[KEY_UP]==1 && key[KEY_LEFT]==0 && key[KEY_DOWN]==0)
{
  ChangeAnimation("Event Actor", "run_northeast", NO_CHANGE);
}
else if (key[KEY_UP]==0 && key[KEY_LEFT]==1 && key[KEY_DOWN]==0)
{
  ChangeAnimation("Event Actor", "stand_west", NO_CHANGE);
}
else if (key[KEY_UP]==0 && key[KEY_LEFT]==0 && key[KEY_DOWN]==1)
{
  ChangeAnimation("Event Actor", "run_southeast", NO_CHANGE);
}
else if (key[KEY_UP]==0 && key[KEY_LEFT]==1 && key[KEY_DOWN]==1)
{
  ChangeAnimation("Event Actor", "run_south", NO_CHANGE);
}
else if (key[KEY_UP]==1 && key[KEY_LEFT]==1 && key[KEY_DOWN]==0)
{
  ChangeAnimation("Event Actor", "run_north", NO_CHANGE);
}
else if (key[KEY_UP]==1 && key[KEY_LEFT]==0 && key[KEY_DOWN]==1)
{
  ChangeAnimation("Event Actor", "run_east", NO_CHANGE);
}


Player -> KeyDown event -> Left (Enabled) -> Script Editor

char* key = GetKeyState();
if (key[KEY_UP]==0 && key[KEY_RIGHT]==0 && key[KEY_DOWN]==0)
{
  ChangeAnimation("Event Actor", "run_west", NO_CHANGE);
}
else if (key[KEY_UP]==1 && key[KEY_RIGHT]==0 && key[KEY_DOWN]==0)
{
  ChangeAnimation("Event Actor", "run_northwest", NO_CHANGE);
}
else if (key[KEY_UP]==0 && key[KEY_RIGHT]==1 && key[KEY_DOWN]==0)
{
  ChangeAnimation("Event Actor", "stand_east", NO_CHANGE);
}
else if (key[KEY_UP]==0 && key[KEY_RIGHT]==0 && key[KEY_DOWN]==1)
{
  ChangeAnimation("Event Actor", "run_southwest", NO_CHANGE);
}
else if (key[KEY_UP]==0 && key[KEY_RIGHT]==1 && key[KEY_DOWN]==1)
{
  ChangeAnimation("Event Actor", "run_south", NO_CHANGE);
}
else if (key[KEY_UP]==1 && key[KEY_RIGHT]==1 && key[KEY_DOWN]==0)
{
  ChangeAnimation("Event Actor", "run_north", NO_CHANGE);
}
else if (key[KEY_UP]==1 && key[KEY_LEFT]==0 && key[KEY_DOWN]==1)
{
  ChangeAnimation("Event Actor", "run_west", NO_CHANGE);
}


Each of these keydown events script for all relevant possible key combinations and shows how to deal with it. They check for conflicting keys, up down, or left right, and for the third key. Not all actual possible combinations are there as all down is not shown, but this combination is irrelevant, partly due to most keyboards only allowing two to three keys of input, and that if all are pressed each code would see how to deal with it. Each event has repeat enabled allowing for a constant flow of possible animation change. Using this flow, we can lighten the key up events, trusting that the animations are the right ones. The keyup events rely on animindex to determine what state the actor is in and then chooses how to react.


Player -> KeyUp event -> Up -> Script Editor

char * key = GetKeyState();
if(animindex == getAnimIndex("run_northeast"))
{
  if(key[KEY_RIGHT]==0)
  {
    ChangeAnimation("Event Actor", "stand_northeast", NO_CHANGE);
  }
  else if(key[KEY_RIGHT]==1)
  {
    ChangeAnimation("Event Actor", "run_east", NO_CHANGE);
  }
}
else if (animindex == getAnimIndex("run_northwest"))
{
  if(key[KEY_LEFT]==0)
  {
    ChangeAnimation("Event Actor", "stand_northwest", NO_CHANGE);
  }
  else if(key[KEY_LEFT]==1)
  {
    ChangeAnimation("Event Actor", "run_west", NO_CHANGE);
  }
}
else if(animindex == getAnimIndex("run_north"))
{
  ChangeAnimation("Event Actor", "stand_north", NO_CHANGE);
}


Player -> KeyUp event -> Down -> Script Editor

char * key = GetKeyState();
if(animindex == getAnimIndex("run_southeast"))
{
  if(key[KEY_RIGHT]==0)
  {
    ChangeAnimation("Event Actor", "stand_southeast", NO_CHANGE);
  }
  else if(key[KEY_RIGHT]==1)
  {
    ChangeAnimation("Event Actor", "run_east", NO_CHANGE);
  }
}
else if (animindex == getAnimIndex("run_southwest"))
{
  if(key[KEY_LEFT]==0)
  {
    ChangeAnimation("Event Actor", "stand_southwest", NO_CHANGE);
  }
  else if(key[KEY_LEFT]==1)
  {
    ChangeAnimation("Event Actor", "run_west", NO_CHANGE);
  }
}
else if(animindex == getAnimIndex("run_south"))
{
  ChangeAnimation("Event Actor", "stand_south", NO_CHANGE);
}


Player -> KeyUp event -> Right -> Script Editor

char * key = GetKeyState();
if(animindex == getAnimIndex("run_northeast"))
{
  if(key[KEY_UP]==0)
  {
    ChangeAnimation("Event Actor", "stand_northeast", NO_CHANGE);
  }
  else if(key[KEY_UP]==1)
  {
    ChangeAnimation("Event Actor", "run_north", NO_CHANGE);
  }
}
else if (animindex == getAnimIndex("run_southeast"))
{
  if(key[KEY_DOWN]==0)
  {
    ChangeAnimation("Event Actor", "stand_southeast", NO_CHANGE);
  }
  else if(key[KEY_DOWN]==1)
  {
    ChangeAnimation("Event Actor", "run_south", NO_CHANGE);
  }
}
else if(animindex == getAnimIndex("run_east"))
{
  ChangeAnimation("Event Actor", "stand_east", NO_CHANGE);
}


Player -> KeyUp event -> Left -> Script Editor

char * key = GetKeyState();
if(animindex == getAnimIndex("run_northwest"))
{
  if(key[KEY_UP]==0)
  {
    ChangeAnimation("Event Actor", "stand_northwest", NO_CHANGE);
  }
  else if(key[KEY_UP]==1)
  {
    ChangeAnimation("Event Actor", "run_north", NO_CHANGE);
  }
}
else if (animindex == getAnimIndex("run_southwest"))
{
  if(key[KEY_DOWN]==0)
  {
    ChangeAnimation("Event Actor", "stand_southwest", NO_CHANGE);
  }
  else if(key[KEY_DOWN]==1)
  {
    ChangeAnimation("Event Actor", "run_south", NO_CHANGE);
  }
}
else if(animindex == getAnimIndex("run_west"))
{
  ChangeAnimation("Event Actor", "stand_west", NO_CHANGE);
}


These check the three possible relevant circumstances that the key up would need to deal with. Since we can rely on the constant flow of keydown events will give the right animation, the keyup event will only need to concern itself with three directions. They first check the animation, and then if the other key having power to make the animation is pressed, if not then it sets it to stand, if so then it changes the run. The last bit of the code required is actual movement. The movement is a simple code moving the actor when a key is pressed.


Player -> Draw Actor -> Script Editor

char * key = GetKeyState();
if(key[KEY_UP]==1)
{
  y-=5;
}
if(key[KEY_DOWN]==1)
{
  y+=5;
}
if(key[KEY_RIGHT]==1)
{
  x+=5;
}
if(key[KEY_LEFT]==1)
{
  x-=5;
}