anti moonwalk explained

Learn how to make certain types of games and use gameEditor.

anti moonwalk explained

Postby Hblade » Fri Aug 03, 2012 10:18 pm

Though there are many types of anti-moonwalking systems, the one I mostly use is this one and I'm going to break it down so that beginners can understand it.

To start off, we'll be using the caveman graphics. add the animations to an actor called "Player" exactly in this order:
Code: Select all
charStopRight
charStopLeft
charRight
charLeft


your going to need to know how animindex works. animindex tells you the animation I.D, from 0 to however many animations you have on that actor. We have 4, so it starts from the top and goes down as it counts up. so like this:
Code: Select all
charStopRight: 0
charStopLeft:  1
charRight: 2
charLeft: 3


You want to write down all of this on a notepad or gedit or what ever text editor you use. Specially if you have a lot of animations. Anyway, lets go into Events -- Draw Actor -- Script Editor. First thing we need to do is allow the key inputs
Code: Select all
char*key=GetKeyState();


This will make it so the player receives keyboard inputs. Now add this command to create a variable called "dir"
Code: Select all
int dir=key[KEY_RIGHT]-key[KEY_LEFT];


This means, that when keyRightArrow is pressed, dir=1, but when keyLeftArrow is pressed, it equals -1, and when none or both are pressed, it equals 0. Now lets make the player move when pressing those keys. using this code.
Code: Select all
x+=(dir*5);


This will make the player move 5 pixels depending on the direction you press. Now lets add a switch statement to determine the animation changes to prevent the moonwalk bug
Code: Select all
switch(dir)
{
    case 0: //none or both keys are pressed;
        switch(animindex)
        {
            case 2: //move right
                ChangeAnimation("Event Actor", "charStopRight", FORWARD);
            break;
            case 3: //move left
                ChangeAnimation("Event Actor", "charStopLeft", FORWARD);
            break;
        }
    break;
    case 1: //right arrow is pressed
        ChangeAnimation("Event Actor", "charRight", NO_CHANGE);
    break;
    case -1: //left arrow is pressed
        ChangeAnimation("Event Actor", "charLeft", NO_CHANGE);
    break;
}


now play test the game :) Enjoy!



Full code to copy & paste:
Code: Select all
char*key=GetKeyState();
int dir=key[KEY_RIGHT]-key[KEY_LEFT];
x+=(dir*5);

switch(dir)
{
    case 0: //none;
        switch(animindex)
        {
            case 2: //move right
                ChangeAnimation("Event Actor", "charStopRight", FORWARD);
            break;
            case 3: //move left
                ChangeAnimation("Event Actor", "charStopLeft", FORWARD);
            break;
        }
    break;
    case 1: //right
        ChangeAnimation("Event Actor", "charRight", NO_CHANGE);
    break;
    case -1: //left
        ChangeAnimation("Event Actor", "charLeft", NO_CHANGE);
    break;
}
Subscribe to my YouTube? - Yes| No
User avatar
Hblade
 
Posts: 4455
Joined: Fri Dec 08, 2006 11:14 pm
Score: 181 Give a positive score

Re: anti moonwalk explained

Postby AliceXIII » Fri Aug 03, 2012 11:04 pm

I like how no matter how much you already know there's always something you miss somewhere:
Code: Select all
int dir=key[KEY_RIGHT]-key[KEY_LEFT];


I never even thought of using keys this way, thank you hblade as you've enlightened me that little bit more +1 to you sir :!:

although i wouldn't call your movement code in the actors draw actor not using draw actors is more cpu efficient less calls instead use a key down event for the specific keys you want and use that code works exactly the same difference is your movement code will be called only when you need it instead of every second of the game..

also you could use any keys in the key down event would work the same i say specify your keys because think about it if you had other keyboard actions to do ie: press 'm' to mute music or something it would call the movement code when it's not needed although it would only read the first 3 lines but still why read them at all if there not used :lol:
"Taking a breath of fresh air."
User avatar
AliceXIII
 
Posts: 325
Joined: Fri Sep 17, 2010 2:36 am
Location: victoria, texas
Score: 37 Give a positive score

Re: anti moonwalk explained

Postby Hblade » Fri Aug 03, 2012 11:53 pm

Come to think of it, this code in keydown would still work. Amazing :) And thanks, alice :) glad to help
Subscribe to my YouTube? - Yes| No
User avatar
Hblade
 
Posts: 4455
Joined: Fri Dec 08, 2006 11:14 pm
Score: 181 Give a positive score

Re: anti moonwalk explained

Postby AliceXIII » Sat Aug 04, 2012 12:18 am

thats what us experts are here for to help everyone else :wink: haha

but im all about optimized use of GE, so anytime i see a better way to do something i don't hesitate to let the person know :)
"Taking a breath of fresh air."
User avatar
AliceXIII
 
Posts: 325
Joined: Fri Sep 17, 2010 2:36 am
Location: victoria, texas
Score: 37 Give a positive score

Re: anti moonwalk explained

Postby Hblade » Sat Aug 04, 2012 3:33 am

Haha yeah =)
Subscribe to my YouTube? - Yes| No
User avatar
Hblade
 
Posts: 4455
Joined: Fri Dec 08, 2006 11:14 pm
Score: 181 Give a positive score

Re: anti moonwalk explained

Postby TheRealMegamanX » Thu Dec 27, 2012 9:56 pm

Question is it supposed to move in place when you enter the code?
TheRealMegamanX
 
Posts: 11
Joined: Wed Dec 26, 2012 4:16 am
Score: 0 Give a positive score

Re: anti moonwalk explained

Postby skydereign » Thu Dec 27, 2012 10:21 pm

It shouldn't, because that code is meant to go into the player's draw actor.
User avatar
skydereign
 
Posts: 3510
Joined: Mon Jul 28, 2008 8:29 am
Score: 589 Give a positive score


Return to Tutorials

Who is online

Users browsing this forum: No registered users and 1 guest