keyboard_clear doesn't seem to work

G

Genjira

Guest
Hello.

I want to force the user to hit space each time they want to jump so they can't just keep space pressed down to carry on jumping. I thought keyboard_clear(vk_space) would do that but it doesn't seem to have any effect. If I hold the space bar down, the player object just keeps jumping. My code is in the player object step event:

Code:
var t1 = tilemap_get_at_pixel(tilemap,bbox_left,bbox_bottom + 1) & tile_index_mask;
var t2 = tilemap_get_at_pixel(tilemap,bbox_right,bbox_bottom + 1) & tile_index_mask;
if (t1 != 0 || t2 !=0) // i.e. on the ground (using the GMWolf Tilemap Collisions technique)
{
	if (keyboard_check(vk_space))
	{
		keyboard_clear(vk_space);
		// set various jumping variables...
	}
}
Am I misunderstanding how (or when) keyboard_clear works?

I've tried using keyboard_check_direct(vk_space) but that doesn't pick up every key press.

Thanks in advance.
 
G

Genjira

Guest
Thanks, jackquake, but that behaves in a similar way to keyboard_check_direct(vk_space) in that it only picks up about a quarter of the space key presses... or I should probably say, my code only responds to about a quarter of them. I'll rethink the step code and start again, I think.

I'm sure this is all standard, basic stuff (walk left, walk right, run, jump, running-jump, fall, etc.). Is there a best practice, minimum code tutorial? I'm cobbling together the My First Game project, the MSWolf tilemap stuff and some other bits and bobs I've found.
 

jackquake

Member
I can offer a couple points:

Tutorial recommendation. I often like Shaun's tutorials because they are very easy to follow and often times, he often answers a question I have just a few seconds after it pops into my head. I haven't watched this particular tutorial, but it could certainly do no harm in watching it.

As for troubleshooting your code above, if it were me, I'd use the show_debug_message() to try to figure out what is going on. If it is intermittent, then neither of the t1 / t2 variables are being set to progress through to the next conditional, which is where you detect the space key press. After editing your code to use this, then look at the console window, where it will continually print out the values of your variables and show you where your issue might be. You can compare these with what is also happening visually and make adjustments. Hope this helps!

Code:
var t1 = tilemap_get_at_pixel(tilemap,bbox_left,bbox_bottom + 1) & tile_index_mask;
var t2 = tilemap_get_at_pixel(tilemap,bbox_right,bbox_bottom + 1) & tile_index_mask;
show_debug_message("t1 = "+string(t1)+"   t2 = "+string(t2));
if (t1 != 0 || t2 !=0) // i.e. on the ground (using the GMWolf Tilemap Collisions technique)
{
   show_debug_message("I'm ready for the space key to be pressed");
   if (keyboard_check_pressed(vk_space))
   {
       show_debug_message("The Space Key was Pressed!");
       // set various jumping variables...
   }
}
 
Last edited:
G

Genjira

Guest
Thanks again, jackquake. I'll try both of those suggestions.
 
Top