help with movement sprite checks

T

temmieflakes42

Guest
this is my first time using gamemaker so sorry for any naive mistakes but, im having some difficulties getting my sprites to change when i move. ive got the movement all set up but the keyboard checks i added in aren't working, could somebody please tell me why?
heres the code:

Code:
//Get the player's input
key_right = keyboard_check(vk_right);
key_left = -keyboard_check(vk_left);
key_jump = keyboard_check_pressed(vk_up);

//React to inputs
move = key_left + key_right;
hsp = move * movespeed;
if (vsp < 10) vsp += grav;

if (place_meeting(x,y+1,obj_wall))
{
    vsp = key_jump * -jumpspeed
}

//Horizontal Collision
if (place_meeting(x+hsp,y,obj_wall))
{
    while(!place_meeting(x+sign(hsp),y,obj_wall))
    {
        x += sign(hsp);
    }
    hsp = 0;
}
x += hsp;

//Vertical Collision
if (place_meeting(x,y+vsp,obj_wall))
{
    while(!place_meeting(x,y+sign(vsp),obj_wall))
    {
        y += sign(vsp);
    }
    vsp = 0;
   
    if(keyboard_check(ord("vk_left"))) then { x -= global.runspeed }
if(keyboard_check(ord("vk_left"))) then sprite_index = sprite_comp_walkleft

if(keyboard_check(ord("vk_right"))) then { x += global.runspeed }
if(keyboard_check(ord("vk_right"))) then sprite_index = sprite_comp_walkright

if(keyboard_check(ord("vk_up"))) then { y -= global.runspeed }
if(keyboard_check(ord("vk_up"))) then sprite_index = sprite_comp_jump

if(keyboard_check(ord("vk_up + vk_right"))) then { y -= global.runspeed }
if(keyboard_check(ord("vk_up + vk_right"))) then sprite_index = sprite_comp_jumpright

if(keyboard_check(ord("vk_up + vk_left"))) then { y -= global.runspeed }
if(keyboard_check(ord("vk_up + vk_left"))) then sprite_index = sprite_comp_jumpleft
}
y += vsp;
 
The first issue I can see here is:
Code:
if(keyboard_check(ord("vk_left"))) then { x -= global.runspeed }
if(keyboard_check(ord("vk_left"))) then sprite_index = sprite_comp_walkleft

if(keyboard_check(ord("vk_right"))) then { x += global.runspeed }
if(keyboard_check(ord("vk_right"))) then sprite_index = sprite_comp_walkright

if(keyboard_check(ord("vk_up"))) then { y -= global.runspeed }
if(keyboard_check(ord("vk_up"))) then sprite_index = sprite_comp_jump

if(keyboard_check(ord("vk_up + vk_right"))) then { y -= global.runspeed }
if(keyboard_check(ord("vk_up + vk_right"))) then sprite_index = sprite_comp_jumpright

if(keyboard_check(ord("vk_up + vk_left"))) then { y -= global.runspeed }
if(keyboard_check(ord("vk_up + vk_left"))) then sprite_index = sprite_comp_jumpleft

ord("vk_up") is not right. It should either be vk_up or ord('w') for example (this applies to all of these keyboard checks). Also, I don't think you can add keys together with the keyboard checks.
 

Erayd

Member
Why are you doing -keyboard_check(vk_left); ? Whats with the negative? If your looking for a keypress then a true and false is all you need, which is what the keyboard_check returns. The negative just gives you the opposite output, so if you hit the button then you get false.

So no, you cannot add key_left and key_right because your just adding a true and a false or whatever your getting at that moment.

As Jordan mentioned above, using ord() in that way is wrong, ord() is meant for specific character key presses like W or 2 while the keycode itself is for things like backspace or left, something a little more complicated.

Near the bottom of your code, you wrote: if(keyboard_check(ord("vk_up + vk_left"))) then { y -= global.runspeed }
This wont work. Try:
if(keyboard_check(vk_up) && keyboard_check(vk_left)) { y -= global.runspeed }
Basically, if up and left then whatever. You dont need the then and you cant add keycodes together because keycodes are just numbers, your then just looking for some other key being pressed, who knows what.
 
T

temmieflakes42

Guest
sorry for failing to respond,
ive got the movement all down now. my friend helped me figure it out and i edited his code to fit mine. thank you very much for your help though!
 
Top