Page 1 of 1

[Tutorial]: Easy Moonwalk Fix

PostPosted: Wed Feb 29, 2012 9:24 pm
by Hblade
Hey guys! Ever wanted to solve the moonwalk but simply? Heres an explanation how.

First, create a new game. Don't try integrating this into your game until you understand how it works. :) Now, create an actor, name him what ever you like. Give him the caveman graphics for this example. ALl 4 animations, charLeft,Right,StopLeft,StopRight. Now add a draw actor and type this
Code: Select all
char*key=GetKeyState():

This will get the key state so you can use the KeyDown events in Draw Actor. Now, create an int using code like this
Code: Select all
int DIR=key[KEY_LEFT]-key[KEY_RIGHT];

This means, when key LEFT is pressed, the value of DIR is 1. When key RIGHT is pressed, the value of DIR is -1. When none or both are pressed, its 0. Now, make a switch statement like this:
Code: Select all
switch(DIR) {
   
}

Now inbetween that, create "cases". First create this one:
Code: Select all
    case -1:
    x+=5;
    ChangeAnimation("Event Actor", "charRight", NO_CHANGE);
    break;


Now this means if the player is pressing right, he'll go right and change his animation to right. Now add another one after the "break". Break means it ends off that case. Anyway, add this under that.
Code: Select all
    case 1:
    x-=5;
    ChangeAnimation("Event Actor", "charLeft", NO_CHANGE);
    break;

This means if left is pressed, he'll go left and walk left. Finally, add a case 0, which means if none or both are pressed, like this
Code: Select all
    case 0:
        if(animindex==getAnimIndex("charRight")) {
                ChangeAnimation("Event Actor", "charStopRight", NO_CHANGE);
                                                 }
        else if(animindex==getAnimIndex("charLeft")) {
                ChangeAnimation("Event Actor", "charStopLeft", NO_CHANGE);
                                                     }
    break;


the part that says if (animindex) means if the animation is Equal to charRight, then it changes to StopRight. Else, it changes to stop Left. Your final code should look like this
Code: Select all
char*key=GetKeyState();
int DIR=key[KEY_LEFT]-key[KEY_RIGHT];

switch(DIR) {
    case -1:
    x+=5;
    ChangeAnimation("Event Actor", "charRight", NO_CHANGE);
    break;
    case 1:
    x-=5;
    ChangeAnimation("Event Actor", "charLeft", NO_CHANGE);
    break;
    case 0:
        if(animindex==getAnimIndex("charRight")) {
                ChangeAnimation("Event Actor", "charStopRight", NO_CHANGE);
                                                 }
        else if(animindex==getAnimIndex("charLeft")) {
                ChangeAnimation("Event Actor", "charStopLeft", NO_CHANGE);
                                                     }
    break;
              }



Hope this helps :)



Skydereign: if you know of another way to get the animindex without using if, please let me know.

Special thanks to Sky and other programmers of GE.

Re: [Tutorial]: Easy Moonwalk Fix

PostPosted: Wed Feb 29, 2012 11:28 pm
by skydereign
The state method is what I use for this kind of thing, but I avoid using getAnimName or getAnimIndex purely by laying out the animation order beforehand. It saves a lot of work and makes things generally cleaner. For instance I always use this basic animation order.
0 = stand right
1 = stand left
2 = run right
3 = run left
4 = jump right
5 = jump left
... and so on
You can use #defines in global code so you don't have to literals. And since in your code you are already writing getAnimName and "charRight" you might as well just use integers (this way you can also use a switch). Another benefit to this kind of ordering is that you can determine which direction the actor is facing, if the animindex is even then it is facing right, odd means it's facing left.
Global Code
Code: Select all
#define charStopRight 0
#define charStopLeft 1
#define charRight 2
#define charLeft 3
// using this you don't have to use getAnimName

And generally I try to avoid using draw for movement code (unless making it extensible to touch control). The main reason is it makes a lot more sense if you use gE's event structure. I've noticed that a lot of users don't fully understand how the events interact with each other, which helps immensely when making games.

Re: [Tutorial]: Easy Moonwalk Fix

PostPosted: Thu Mar 01, 2012 3:18 am
by Hblade
skydereign wrote:The state method is what I use for this kind of thing, but I avoid using getAnimName or getAnimIndex purely by laying out the animation order beforehand. It saves a lot of work and makes things generally cleaner. For instance I always use this basic animation order.
0 = stand right
1 = stand left
2 = run right
3 = run left
4 = jump right
5 = jump left
... and so on
You can use #defines in global code so you don't have to literals. And since in your code you are already writing getAnimName and "charRight" you might as well just use integers (this way you can also use a switch). Another benefit to this kind of ordering is that you can determine which direction the actor is facing, if the animindex is even then it is facing right, odd means it's facing left.
Global Code
Code: Select all
#define charStopRight 0
#define charStopLeft 1
#define charRight 2
#define charLeft 3
// using this you don't have to use getAnimName

And generally I try to avoid using draw for movement code (unless making it extensible to touch control). The main reason is it makes a lot more sense if you use gE's event structure. I've noticed that a lot of users don't fully understand how the events interact with each other, which helps immensely when making games.


Thank you very much sky. +1

Re: [Tutorial]: Easy Moonwalk Fix

PostPosted: Wed Apr 11, 2012 12:59 am
by master0500
using HBlades method, how would i add jumping?

Re: [Tutorial]: Easy Moonwalk Fix

PostPosted: Wed Apr 11, 2012 1:34 am
by Hblade
by adding an if statement before the change animation to walking, example:
Code: Select all
if(jump)
{
    change animation to jump right/left
}
else {
    change animation to walk right/left
}


Same with the case 0: which is none, you'd then do stopleft and fallleft or something

I reccomend sky's code though for that, as his is shorter.