[Tutorial]: Easy Moonwalk Fix

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

[Tutorial]: Easy Moonwalk Fix

Postby Hblade » Wed Feb 29, 2012 9:24 pm

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.
Subscribe to my YouTube? - Yes| No
User avatar
Hblade
 
Posts: 4345
Joined: Fri Dec 08, 2006 11:14 pm
Score: 167 Give a positive score

Re: [Tutorial]: Easy Moonwalk Fix

Postby skydereign » Wed Feb 29, 2012 11:28 pm

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.
User avatar
skydereign
 
Posts: 3395
Joined: Mon Jul 28, 2008 8:29 am
Score: 565 Give a positive score

Re: [Tutorial]: Easy Moonwalk Fix

Postby Hblade » Thu Mar 01, 2012 3:18 am

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
Subscribe to my YouTube? - Yes| No
User avatar
Hblade
 
Posts: 4345
Joined: Fri Dec 08, 2006 11:14 pm
Score: 167 Give a positive score

Re: [Tutorial]: Easy Moonwalk Fix

Postby master0500 » Wed Apr 11, 2012 12:59 am

using HBlades method, how would i add jumping?
master0500
 
Posts: 404
Joined: Sun Jun 26, 2011 9:42 pm
Score: 27 Give a positive score

Re: [Tutorial]: Easy Moonwalk Fix

Postby Hblade » Wed Apr 11, 2012 1:34 am

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.
Subscribe to my YouTube? - Yes| No
User avatar
Hblade
 
Posts: 4345
Joined: Fri Dec 08, 2006 11:14 pm
Score: 167 Give a positive score


Return to Tutorials

Who is online

Users browsing this forum: No registered users and 1 guest

cron