Page 1 of 1

Perfect Fix for a moonwalk

PostPosted: Mon Aug 01, 2011 6:00 pm
by Hblade
Hello! Tired of that moonwalk? Tired of typing loads of code to fix it? Here you go! First, copy this code to your games Global Code
Code: Select all
//Star Functions Version 1.0//
              /////////////
////////////// Definitions /////////////////////
             //////////////
             #define KEYRIGHT key[KEY_d]
             #define KEYLEFT key[KEY_a]
             #define KEYUP key[KEY_w]
             #define KEYDOWN key[KEY_d]
             #define KEYA key[KEY_k]
             #define KEYB key[KEY_l]
             #define KEYX key[KEY_j]
             #define KEYY key[KEY_i]
             #define KEYSTART key[KEY_RETURN]
             #define KEYSELECT key[KEY_BACKSPACE]
             #define KEYLB key[KEY_q]
             #define KEYRB key[KEY_e]
 
             #define JoyA JoyBtn[0]
             #define JoyB JoyBtn[1]
             #define JoyX JoyBtn[2]
             #define JoyY JoyBtn[3]
             #define JoyLB JoyBtn[4]
             #define JoyRB JoyBtn[5]
             #define JoyStart JoyBtn[7]
             #define JoySelect JoyBtn[6]
 
             #define GORIGHT ((LXAXIS>los) || (KEYRIGHT==1 && KEYLEFT==0))
             #define GOLEFT ((LXAXIS<-los) || (KEYLEFT==1 && KEYRIGHT==0))
             #define GOUP ((LYAXIS<-los) || (KEYUP==1 && KEYDOWN==0))
             #define GODOWN ((LYAXIS>los) || (KEYDOWN==1 && KEYUP==0))
             #define APRESS (JoyA==1 || KEYA==1)
             #define BPRESS (JoyB==1 || KEYB==1)
             #define XPRESS (JoyX==1 || KEYX==1)
             #define YPRESS (JoyY==1 || KEYY==1)
             #define LBPRESS (JoyLB==1 || KEYLB==1)
             #define RBPRESS (JoyRB==1 || KEYRB==1)
             #define START (JoyStart==1 || KEYSTART==1)
             #define SELECT (JoySelect==1 || KEYSELECT==1)
 
 
 
              ////////////
////////////// Variables //////////////////////
             ////////////


//integers//
int VARS[2000]; //Create 2000 in-game non-actor variables
int timer[150]; //Create 150 timer variables for timing
int JoyBtn[15]; //Support for up to 15 joystick buttons


//Character variables//
char *key; //Used for get key state in the code//

//Doubles//
double los; //Left joystick offset
double ros; //Right joystick offset
double LXAXIS, LYAXIS, RXAXIS, RYAXIS; //This creates the axis variables for both the left and right joysticks.

 
 
 
             /////////////
///////////// Get Gamepad /////////////////////
             ////////////

void GetPad(double LOS, double ROS) {
    int i;
    los=LOS;
    ros=ROS;
    for (i=0;i<15;i++) {
        JoyBtn[i]=GetJoystick1Button(i); } //This gets all 15 buttons
    LXAXIS=GetJoystick1Axis(0);
    LYAXIS=GetJoystick1Axis(1);
    RXAXIS=GetJoystick1Axis(2);
    RYAXIS=GetJoystick1Axis(3);
                                    }
 
             ///////////////
///////////// Get Keyboard /////////////////////
             //////////////

void GetKeys() {
    key=GetKeyState(); }

now dont worry its as simple as 1, 2, 3 to fix this moonwalk glitch ^^ As you will see in the code, this also supports controllers so you can easily be able to use one. You can configure your controls in the #define area at the top of the code. Currently the script by defualt uses ASWD to move and K, I, J, L to be acting as "ABXY" or "X,O, Box, Triangle" would on a controller. (Or 0123/ 1234 depending on the controller your using). Anyways lets get down to fixing the moon walk :) After you pasted this code, give it a name then click "Add". Now go into your players Draw Actor and put this:
Code: Select all
GetPad(5000, 5000); GetKeys();

This will get both the keyboard and the controller. The 5000 is the offset for the controller so that if you just barely touch the joystick the character wont move. This prevents the player from moving when the joystick is just BARELY touched or left alone.

Now to make your character move, its as simple as this!
Code: Select all
if (GORIGHT) {
    x+=5;
    ChangeAnimation("Event Actor", "RunRight", NO_CHANGE);
             }

Thats right! As simple as "GORIGHT". Now use the same for goleft
Code: Select all
if (GOLEFT) {
    x-=5;
    ChangeAnimation("Event Actor", "RunLeft", NO_CHANGE);
            }

(Of course youll need to modify the "ChangeAnimation".

To stop the player when both left and right are active:
Code: Select all
if (KEYRIGHT==1 && KEYLEFT==1) {
    if (animindex==0) {
        ChangeAnimation("Event Actor", "StopLeft", NO_CHANGE); }
    if (animindex==1) {
        ChangeAnimation("Event Actor", "StopRight", NO_CHANGE); }
                               }

Heres where it could get a little tricky. animindex is the animation your player is in, so you might need to use a variable to determine which animindex is "moveright" and "moveleft". Mine is 0 and 1, the first animation of the player is MoveLeft, then it's MoveRight, so therefor the animindex's are 0 and 1. Now to stop the player when the player isn't pressing any movement keys or joystick movement.
Code: Select all
if ((LXAXIS<los && LXAXIS>-los) && KEYLEFT==0 && KEYRIGHT==0) {
    if (animindex==0) {
        ChangeAnimation("Event Actor", "StopLeft", NO_CHANGE); }
    if (animindex==1) {
        ChangeAnimation("Event Actor", "StopRight", NO_CHANGE); }
                                                              }


There ya have it, no more moonwalking :)



Quotes from: http://game-editor.com/docs/script_reference.htm
"animindex: Use animindex(count from 0) to find the actual animation of your actor. Each animation that an actor has has a unique index assigned to it. ( This way you can tell which animation is currently running. )

If your actor has 3 animations, then:

The first animation has animindex = 0
The second animation has animindex = 1
The third animation has animindex = 2

This variable is read only."