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

My character keeps facing right whenever I press any other key besides the directional keys (WASD)

G

gbcd

Guest
Whenever I press any other key besides the WASD keys I coded in, my character suddenly faces right. And when I keep whatever key pressed, his right direction walking animation starts playing, but he stays in place.

Please forgive my noob codes:

GML:
move_up    = keyboard_check(ord("W"))
move_left  = keyboard_check(ord("A"))
move_down  = keyboard_check(ord("S"))
move_right = keyboard_check(ord("D"))
  
if(keyboard_check(vk_nokey))
{
    image_index = 0
}
else
{ 
    horizontal_input = move_right - move_left
    vertical_input   = move_down - move_up

    sprite_direction = point_direction(0, 0, horizontal_input, vertical_input)

    move_to_x = lengthdir_x(move_speed, sprite_direction)
    move_to_y = lengthdir_y(move_speed, sprite_direction)

    if(move_right and place_free(x + collision_speed, y)) or (move_left and place_free(x - collision_speed, y))
    {
        x += move_to_x
    }
  
    if(move_up and place_free(x, y - collision_speed)) or (move_down and place_free(x, y + collision_speed))
    {
        y += move_to_y
    }

    switch(sprite_direction)
    {
        case 0:   sprite_index = sWalkingRight break
        //case 45:  break
        case 90:  sprite_index = sWalkingUp break
        //case 135: break
        case 180: sprite_index = sWalkingLeft break
        //case 225: break
        case 270: sprite_index = sWalkingDown break
        //case 315: break
    }
}
Any help is appreciated. Thank you.
 

Slyddar

Member
if(move_right and place_free(x + collision_speed, y)) or (move_left and place_free(x - collision_speed, y))
I haven't seen that method before, and don't think that will work. Firstly, shouldn't collision_speed be move_to_x, and y the same? Also the value of move_to_x and move_to_y can be negative and positive, so you can't do the test you are doing. You can simplify your method by just doing this, assuming you are ticking the solid box on the collision objects.
GML:
    if(place_free(x + move_to_x, y))
    {
        x += move_to_x
    }

    if(place_free(x, y + move_to_y))
    {
        y += move_to_y
    }
For your sprite problem, the sprite is wrong since when you are not moving sprite_direction is set to 0. You should only update sprite_direction if horizontal_input != 0 or vertical_input != 0. Probably don't need the no_key section at all. Something like this:
Note that the input and move_to variables could all be local vars if you don't need them elsewhere.
GML:
if horizontal_input != 0 or vertical_input != 0 {
    sprite_direction = point_direction(0, 0, horizontal_input, vertical_input)
}

if horizontal_input != 0 move_to_x = lengthdir_x(move_speed, sprite_direction) else move_to_x = 0;
if vertical_input != 0 move_to_y = lengthdir_y(move_speed, sprite_direction) else move_to_y = 0;
It's worth noting that using this type of collision system is not great. To demonstrate, put your move_speed to 10 or higher. The player will never get too close to a solid. They can be up to (move_speed -1) pixels away, as there is no code that moves them close to it when the place is not free.
 
Last edited:
G

gbcd

Guest
Thank you guys, I've already figured out what was wrong. TheouAegis is correct that vk_nokey shouldn't be used like this, like in my case. For some reason, the way I used vk_nokey in the if-else statement caused it to check also if all other keys were being pressed even though I didn't have any codes for that.
 
Top