Momoro
Member
Hello
I work on my projects in GameMaker 8.1, because I prefer to use it and I do not currently own GameMaker: Studio 2.
I've put together a script that is supposed to be put into the Step event of an object (the player), and it works unless multiple keys are pressed,
..which I'd thought I took care of inside of the code?
The global variables are set to false when the player is first created.
So, if I press Up, Down, Left or Right separately, it works fine, but if I press them simultaneously, it glitches and it does one of these two glitches every time:
1) It will not let the player move the object unless you figure out which key went wrong, which is different in almost every case.
2) The object will not stop moving, even if the player releases the correct key, until they press and/or release another key simultaneously.
Could anyone correct my code? I'm not seeing the problem, but I'm sure one of you can / do
Thanks for your time!
I work on my projects in GameMaker 8.1, because I prefer to use it and I do not currently own GameMaker: Studio 2.
I've put together a script that is supposed to be put into the Step event of an object (the player), and it works unless multiple keys are pressed,
Code:
if (keyboard_check_pressed(vk_up))
{
if (global.goingDown != true && global.goingRight != true && global.goingLeft != true)
{
global.goingUp = true;
sprite_index = spr_rpg_boy_walk_up;
image_speed = 3;
motion_set(90,3);
}
}
else if (keyboard_check_pressed(vk_down))
{
if (global.goingUp != true && global.goingRight != true && global.goingLeft != true)
{
global.goingDown = true;
sprite_index = spr_rpg_boy_walk_down;
image_speed = 3;
motion_set(270,3);
}
}
else if (keyboard_check_pressed(vk_right))
{
if (global.goingUp != true && global.goingDown != true && global.goingLeft != true)
{
global.goingRight = true;
sprite_index = spr_rpg_boy_walk_right;
image_speed = 3;
motion_set(0,3);
}
}
else if (keyboard_check_pressed(vk_left))
{
if (global.goingUp != true && global.goingDown != true && global.goingRight != true)
{
global.goingLeft = true;
sprite_index = spr_rpg_boy_walk_left;
image_speed = 3;
motion_set(180,3);
}
}
else if (keyboard_check_released(vk_up))
{
global.goingUp = false;
if (keyboard_check_pressed(vk_down))
{
global.goingDown = true;
sprite_index = spr_rpg_boy_walk_down;
image_speed = 3;
motion_set(270,3);
}
else if (keyboard_check_pressed(vk_right))
{
global.goingRight = true;
sprite_index = spr_rpg_boy_walk_right;
image_speed = 3;
motion_set(0,3);
}
else if (keyboard_check_pressed(vk_left))
{
global.goingLeft = true;
sprite_index = spr_rpg_boy_walk_left;
image_speed = 3;
motion_set(180,3);
}
else
{
sprite_index = spr_rpg_boy_up;
image_speed = 3;
motion_set(270,0);
}
}
else if (keyboard_check_released(vk_down))
{
global.goingDown = false;
if (keyboard_check_pressed(vk_up))
{
global.goingUp = true;
sprite_index = spr_rpg_boy_walk_up;
image_speed = 3;
motion_set(90,3);
}
else if (keyboard_check_pressed(vk_right))
{
global.goingRight = true;
sprite_index = spr_rpg_boy_walk_right;
image_speed = 3;
motion_set(0,3);
}
else if (keyboard_check_pressed(vk_left))
{
global.goingLeft = true;
sprite_index = spr_rpg_boy_walk_left;
image_speed = 3;
motion_set(180,3);
}
else
{
sprite_index = spr_rpg_boy_1;
image_speed = 3;
motion_set(270,0);
}
}
else if (keyboard_check_released(vk_right))
{
global.goingRight = false;
if (keyboard_check_pressed(vk_down))
{
global.goingDown = true;
sprite_index = spr_rpg_boy_walk_down;
image_speed = 3;
motion_set(270,3);
}
else if (keyboard_check_pressed(vk_up))
{
global.goingUp = true;
sprite_index = spr_rpg_boy_walk_up;
image_speed = 3;
motion_set(90,3);
}
else if (keyboard_check_pressed(vk_left))
{
global.goingLeft = true;
sprite_index = spr_rpg_boy_walk_left;
image_speed = 3;
motion_set(180,3);
}
else
{
sprite_index = spr_rpg_boy_right;
image_speed = 3;
motion_set(270,0);
}
}
else if (keyboard_check_released(vk_left))
{
global.goingLeft = false;
if (keyboard_check_pressed(vk_down))
{
global.goingDown = true;
sprite_index = spr_rpg_boy_walk_down;
image_speed = 3;
motion_set(270,3);
}
else if (keyboard_check_pressed(vk_right))
{
global.goingRight = true;
sprite_index = spr_rpg_boy_walk_right;
image_speed = 3;
motion_set(0,3);
}
else if (keyboard_check_pressed(vk_up))
{
global.goingUp = true;
sprite_index = spr_rpg_boy_walk_up;
image_speed = 3;
motion_set(90,3);
}
else
{
sprite_index = spr_rpg_boy_left;
image_speed = 3;
motion_set(270,0);
}
}
The global variables are set to false when the player is first created.
So, if I press Up, Down, Left or Right separately, it works fine, but if I press them simultaneously, it glitches and it does one of these two glitches every time:
1) It will not let the player move the object unless you figure out which key went wrong, which is different in almost every case.
2) The object will not stop moving, even if the player releases the correct key, until they press and/or release another key simultaneously.
Could anyone correct my code? I'm not seeing the problem, but I'm sure one of you can / do
Thanks for your time!