R
Raya
Guest
I am trying to make a simple game where the player, represented by a character object called o_tommy, moves left and right with the keyboard arrows. That works but the problem is that I show a message when my timer runs out that aims to inform the player of their score and tell them to try again. Clicking ok on the message restarts the room and works fine as long as the player has clicked the keyboard buttons left and right in a single clicks manner. If the player has held down the left or right keyboard button then when the room restarts the object o_tommy (though newly generated) keeps moving in the direction of the lastly held down key (e.g. right) until it gets out of the room and never comes back.
How can I check if the keyboard key is pressed down continuously and somehow cancel it after clicking ok on the popped up message?
My code for making the object move is located in the o_tommy object Step event:
if (keyboard_check(vk_right)) {
x = x + 14;
}
if (keyboard_check(vk_left)) {
x = x - 14;
}
I restart the room in another object called o_dropper which is an invisible object placed in the room (dragged there in the room editor window) in order to drop objects simultaneously and also in the o_dropper's Create event an o_tommy object gets created so that it gets restarted with room_restart(). In o_dropper's Step event I check the timer and restart the room like this:
if (timer == 0)
{
show_message("bla bla try again");
timer = room_speed * 30;
score = 0;
room_restart();
}
It all works as I want except when the player holds down the left or right key while playing (which is the most natural thing to do). Then o_tommy object, though restarted (shown in initial position) keeps moving in the direction of the lastly held button and nothing can stop it! Holding down the opposite direction key only makes it pause but releasing it the object still keeps moving in that predefined direction (from a key held down prior to the message showing).
I am new to Game Maker, I tried a bunch of alternatives like for example having e.g. an global_pause variable to encompass the moving code in o_tommy' Step but changing the value in o_dropper when timer ran out doesn't stop this left-over movement...
Any advice on how to cancel this movement (of my object leaving the room by himself and running endlessly in the nothingness) will be greatly appreciated
How can I check if the keyboard key is pressed down continuously and somehow cancel it after clicking ok on the popped up message?
My code for making the object move is located in the o_tommy object Step event:
if (keyboard_check(vk_right)) {
x = x + 14;
}
if (keyboard_check(vk_left)) {
x = x - 14;
}
I restart the room in another object called o_dropper which is an invisible object placed in the room (dragged there in the room editor window) in order to drop objects simultaneously and also in the o_dropper's Create event an o_tommy object gets created so that it gets restarted with room_restart(). In o_dropper's Step event I check the timer and restart the room like this:
if (timer == 0)
{
show_message("bla bla try again");
timer = room_speed * 30;
score = 0;
room_restart();
}
It all works as I want except when the player holds down the left or right key while playing (which is the most natural thing to do). Then o_tommy object, though restarted (shown in initial position) keeps moving in the direction of the lastly held button and nothing can stop it! Holding down the opposite direction key only makes it pause but releasing it the object still keeps moving in that predefined direction (from a key held down prior to the message showing).
I am new to Game Maker, I tried a bunch of alternatives like for example having e.g. an global_pause variable to encompass the moving code in o_tommy' Step but changing the value in o_dropper when timer ran out doesn't stop this left-over movement...
Any advice on how to cancel this movement (of my object leaving the room by himself and running endlessly in the nothingness) will be greatly appreciated