• Hey Guest! Ever feel like entering a Game Jam, but the time limit is always too much pressure? We get it... You lead a hectic life and dedicating 3 whole days to make a game just doesn't work for you! So, why not enter the GMC SLOW JAM? Take your time! Kick back and make your game over 4 months! Interested? Then just click here!

(solved) Problem movement sprites in first top-view RPG game.

L

Lakor

Guest
I'm making my first topview adventure. I have some issues with the sprites when walking. I have four sprites: Standing_Left, Walking_Left (strip), Standing_Right and Walking_Right (strip).

You can move all 4 directions, but I only want to use left & right looking sprites (both standing still and walking).

Horizontally the code works perfect, but vertical walking has some issues. When I walk straight down it only uses the Standing sprites and when moving straight up it automatically goes to Walking_Right (even when starting left).

Help much appreciated!

Code:
face = round(dir/180);
 switch(face) {  
 
case 0:     if (xaxis == 0) && (yaxis == 0) {        
sprite_index = spr_standing_right;         }
else { sprite_index = spr_running_right;}        
break;    

case 1:     if  (xaxis == 0) && (yaxis == 0) {       
 sprite_index = spr_standing_left;         }
else { sprite_index = spr_running_left;}       
break;
 
Last edited by a moderator:
T

Teknopants

Guest
Would you mind posting the vertical walking code?
My only guess from what I see here is that maybe face = round(dir/180) is only ever returning limited results, since round(90/180) (up) returns 0 (right) and round(270/180) (down) returns 2
 
L

Lakor

Guest
Would you mind posting the vertical walking code?
My only guess from what I see here is that maybe face = round(dir/180) is only ever returning limited results, since round(90/180) (up) returns 0 (right) and round(270/180) (down) returns 2
I already talked a bit about it on the discord and we came to similar conclusions, though don't know what to add to fix it. Using a tutorial as guidelines, so I'm complete noob when it comes to GML.

Code:
hspd = 0;
vspd = 0;
len = 0;
dir = 0;
face =0;

dir = point_direction(0, 0, xaxis, yaxis);
xaxis = (rKey - lKey);
yaxis = (dKey - uKey);


hspd = lengthdir_x(len, dir);
vspd = lengthdir_y(len, dir);
 
Z

Zachary_tzy

Guest
I already talked a bit about it on the discord and we came to similar conclusions, though don't know what to add to fix it. Using a tutorial as guidelines, so I'm complete noob when it comes to GML.

Code:
hspd = 0;
vspd = 0;
len = 0;
dir = 0;
face =0;

dir = point_direction(0, 0, xaxis, yaxis);
xaxis = (rKey - lKey);
yaxis = (dKey - uKey);


hspd = lengthdir_x(len, dir);
vspd = lengthdir_y(len, dir);
A fix could be just taking into consideration the straight up and straight down cases something like
Code:
face = round(dir/180);
 switch(face) {

case 0:     if (xaxis == 0) && (yaxis == 0) {    
sprite_index = spr_standing_right;        // <-- this is the reason your sprite is standing right when your face == 0 while walking down
 // I assume you either press d or u when walking upward so (yaxis==0) = false and it goes to the  next conditional statement
}
// else { sprite_index = spr_running_right;}     which is this hence when you walk up it becomes this sprite
// instead you might want to do this
else if (yaxis != 0) { sprite_index = *What running sprite you want* } // this additional conditional statement checks if your yaxis has an input and                                                                                //changes it to a walking forward sprite
else { sprite_index = spr_running_right;}
break;

case 1:     if  (xaxis == 0) && (yaxis == 0) {    
 sprite_index = spr_standing_left;         }
else { sprite_index = spr_running_left;}    
break;
and just do the same for case 1 depending on what inputs you are using to move down. Sorry if i'm mistaken because I can't really tell what effect you want to achieve with your code and what kind of movement you are going for. But I hope this helps!
 
L

Lakor

Guest
You remember the old Double Dragon game? Basically a platformer (left-right) but with the option to go up and down a bit as well (though the player only has left-right sprites, not up-down). That is the effect I'm going for.

Unfortunately the extra line does not make any differene. Is there a way around it without using round, maybe just keys?
 

3dgeminis

Member
Try this:
Code:
xaxis = (rKey - lKey);

if xaxis!=0 {sprite_side=xaxis}

if sprite_side=1
   {
    if (xaxis=1 or yaxis!=0) {sprite_index=spr_running_right}
    if (xaxis=0 and yaxis=0) {sprite_index=spr_standing_right}
   }
   
if sprite_side=-1
   {
    if (xaxis=-1 or yaxis!=0)  {sprite_index=spr_running_left}
    if (xaxis=0 and yaxis=0) {sprite_index=spr_standing_left}
   }
The sprite_side variable only changes value when the character moves to the right or left.
 
Z

Zachary_tzy

Guest
You remember the old Double Dragon game? Basically a platformer (left-right) but with the option to go up and down a bit as well (though the player only has left-right sprites, not up-down). That is the effect I'm going for.

Unfortunately the extra line does not make any differene. Is there a way around it without using round, maybe just keys?
I googled double dragon game and yeah you don't really have to use a round if you are trying to achieve that movement style because it is just in 4 directions. If you want just keys you can try this
Code:
xaxis = keyboard_check(ord("A")) - keyboard_check(ord("D")) // If checked will return 1, if not return 0
yaxis = keyboard_check(ord("W")) - keyboard_check(ord("S"))
if xaxis == 0 && yaxis == 0 // Meaning either both A & D / W & S are pressed or both are not pressed, meaning no movement
{
    if direction == 0
    {
        sprite_index = spr_standing_right
    }
    else if direction == 180
    {
        sprite_index = spr_standing_left
    }
}
else if xaxis == 1 // Meaning A is pressed
{
    direction = 0                 // Set direction to 0 which is right
    sprite_index = spr_running_left
}
else if xaxis == -1                 // Meaning D is pressed
{
    direction = 180             // Set direction to 180 which is left
    sprite_index = spr_running_right
}
else if yaxis == 1 || yaxis == -1     // If A & D input is 0 but W & S is not 0,                    
{                    // it will reach this conditional statement
    if direction = 0
    {
        sprite_index = spr_running_right
    }
    else if direction = 180
    {
        sprite_index = spr_running_left
    }
}
It's not the most elegant code but since I assume you are relatively new to gml or programming it's better if you use code that you can understand first. If it works, you can streamline it afterward. I assume your Up and Down keys won't have to change the sprite if its like the double dragon game. I haven't tested it but it should work, let me know if it doesn't I haven't slept in forever. Hope it helps!
 
L

Lakor

Guest
Yes, it worked! I'll stick to the less elegant code for now, because it's easier to learn indeed. Thanks all, much appreciated! :)
 
Top