• 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!

GML Need help with diagonal movement sprite

A

anthropus

Guest
Hi all, im making a top down (non-commercial) fan game (realtime final fantasy 3/6!) to learn gml and have fun.

I'm trying to make the player have a legend of zelda link to the past style of movement where if link is facing up or down and walks diagonally he keeps the walking up/down sprite, and if hes facing left or right and walks diagonally he keeps the left/right walking sprite.
EDIT: like this
END EDIT

So in my move state/script i added this code, but even with it the game itself plays as if this code does not exist at all--it changes nothing that I can see.
Code:
if (if currently_facing==facing.upward && up && right)
{
sprite_index=sp_celes_walk_up;
image_speed=img_spd;
currently_facing=facing.upward;
}
here the the whole move script
Code:
//MOVEMENT

img_spd = .3;
spd=2;

//make the player cast magic
if (spell_button) {current_state=celes_state.cast_magic}

//move the player
if (up)
{
vspd=-spd;
sprite_index=sp_celes_walk_up;
image_speed=img_spd;
currently_facing=facing.upward;
}


if (down)
{
vspd=spd;
sprite_index=sp_celes_walk_down;
image_speed=img_spd;
currently_facing=facing.downward;
}

if (left)
{
hspd=-spd;
sprite_index=sp_celes_walk_left;
image_speed=img_spd;
currently_facing=facing.leftward;
}

if (right)
{
hspd=spd;
sprite_index=sp_celes_walk_right;
image_speed=img_spd;
currently_facing=facing.rightward;
}

//make player not move if no input
if (!up && !down) {vspd=0;}
if (!left && !right) {hspd=0;}

//stop animation for idle
if (!up && !down && !left && !right)
{
current_state=celes_state.idle;
}

//make walk up and sprite play
//for when moving diagonally if previously
//moving up and down
if (currently_facing==facing.upward && up && right)
{
sprite_index=sp_celes_walk_up;
image_speed=img_spd;
}

scr_collision();

If I remove the "currently_facing" part of the code (see below), then what you would expect to happen happens--when walking diagonally up and to the left, the walking up sprite plays, but I only want that sprite to play if the player is facing up or is already walking up (i dont want this to happen if they are facing left or right or if they are walking to the left or right).
Code:
if (up && right)
{
sprite_index=sp_celes_walk_up;
image_speed=img_spd;
currently_facing=facing.upward;
}



The variable "currently_facing" is set in the create event and uses an enumerator (for the different directions), and that variable works fine in my idle state/script so i dont think theres a problem with the variable itself. but when i put that variable in my code the code seems to be rendered non-functional.

I can show more code if needed, i just didnt want to overload this post with code

PLEASE HELP! I've been racking my brains to days!
 
Last edited by a moderator:
Also, to be perfectly honest, I've looked at a LOT of these style of games, and your first example is what most of them do. When moving up or down only, you see the up and down sprite. When moving left or right or any angle, you see the side view. It looks totally fine.
 
M

mochipon

Guest
I don't really understand the way you're trying to achieve it, but it seems very complicated to me.
Why not just make a variable like "facing =2" where 2 is down, 4 is left and so on. facing = 2 is only set if vspd is == -1 I guess, and hsp is == 0, and then 4 is only set when hspd is == 0 and vspd is == -1 and so on.
As long those two conditions aren't met (meaning the player is walking diagonally and hence both hsp and vsp aren't 0) the facing variables won't change. Then you only have to set the player sprite depending on the facing variable. (Or you could combine it into 1).

That way there wouldn't be any code that the game could omit. :p
 
A

anthropus

Guest
Whenever you have to ask yourself "is this code even being run?" then you should use the debugger to determine whether it is being ran or not.
hi and thanks, this does help narrow down the problem. i ran it in debug mode and put a stop at the line of code that has the if statement:
Code:
if (currently_facing=facing.upward && up && right) //the code break/stop is on this line
{
sprite_index=sp_celes_walk_up;
image_speed=img_spd;
current_state=facing.upward;
}
and as you can see there are 3 conditions that have to be met for the "then" part of the code to run: the variable "currently_facing" has to be "facing.upward," the player has to be pushing the up button, and the player has to be pushing the right button, all at the same time.

ok so heres what happens--its weird!: if i press "up" the break stops the game there and the debug window opens and i can see that indeed the currently_facing variable is set to facing.upward, so that shows me that those 2 conditions are being met (pressing up and the player is facing the right direction), but if i press "right" the break does not stop the code and start the debug menu. so it appears that the variable "right" is not working--but that doesnt make sense bc the player is moving to the right (up and right)! ALL CONDITIONS OF THE IF STATEMENT ARE BEING MET, HWY ISNT THE CODE EXECUTING!?!?

:( further more, if i switch the position of the "up" and "right" so that the if statement reads like this if ((currently_facing=facing.upward && right && up) [instead of "up && right"] the break on the code never stops the code in debug mode.

so im concluding that im not using the "&&" correctly (or the whole if (...) us not written correctly) is but if so i have no idea why :(

i cant figure this out! HELP!!!
 
Everything looks correct. You are using && correctly. I would probably need to see the project to debug it, as I imagine something is messing with your "right" variable that you don't realize.
 
A

anthropus

Guest
Also, to be perfectly honest, I've looked at a LOT of these style of games, and your first example is what most of them do. When moving up or down only, you see the up and down sprite. When moving left or right or any angle, you see the side view. It looks totally fine.
i know what your saying but 1) i dont like it like that lol; and 2) i really want to learn GML, thats more or less the point of this, and if something doesnt work, i really want to understand why

I don't really understand the way you're trying to achieve it, but it seems very complicated to me.
Why not just make a variable like "facing =2" where 2 is down, 4 is left and so on. facing = 2 is only set if vspd is == -1 I guess, and hsp is == 0, and then 4 is only set when hspd is == 0 and vspd is == -1 and so on.
As long those two conditions aren't met (meaning the player is walking diagonally and hence both hsp and vsp aren't 0) the facing variables won't change. Then you only have to set the player sprite depending on the facing variable. (Or you could combine it into 1).

That way there wouldn't be any code that the game could omit. :p
mochipon, i pretty much did just that, i made an enumerator to help manage the different directions the player is facing bc this is important and will affect other things down the road in the game's development.

this is the kind of diagonal movement im trying to achieve

any other suggestions? thanks
 
A

anthropus

Guest
Everything looks correct. You are using && correctly. I would probably need to see the project to debug it, as I imagine something is messing with your "right" variable that you don't realize.
thanks for that :) ppl like you give me hope for the world! is there a way you could possibly see my project? its not that big a file atm. is there a way i can send it to you perhaps?

thanks again!
 
Top