[SOLVED] Mouse_check inconsistency when using keyboard checks

M

Mark M

Guest
Hi everyone,

I've recently started using GameMaker Studio 2. I'm playing around with a basic 2D platformer to get used to GML etc... But I've run into a problem when combining keyboard/mouse checks and collision detection.

On startup, the bullets shoot correctly when the mouse is pressed. If I hold the mouse key I can also move and jump etc. The problem is when I move/jump before clicking the mouse button. The bullet does not appear to be created and it takes several frames before the mouse click is registered again.

The movement and collision detection is all taken from the platform tutorial. I've simply tried to add a mouse_check to create a new instance.

I'm probably missing something obvious but a fresh pair of eyes might help. I can't see why it's not working!

Player "Create" Event:
Code:
player_speed = 4;
player_jump_power = 9;
base_gravity = 0.2;
horiz_spd = 0;
vert_spd = 0;
shot_cooldown = 0;
Player "Step" Event:
Code:
mouse_left = mouse_check_button(mb_left);
key_right = keyboard_check(ord("D"));
key_left = -keyboard_check(ord("A"));
key_up = keyboard_check_pressed(ord("W"));

//fire check
if(mouse_left){
    instance_create_layer(x,y,"bullet_layer",obj_bullet);
}
//movement
move = key_right + key_left;
horiz_spd = move * player_speed;
if(vert_spd < 10){
    vert_spd += base_gravity;
}

if (place_meeting(x,y+1,obj)){
    vert_spd = key_up * -player_jump_power;
}

//collision checks
if(place_meeting(x+horiz_spd,y,obj_terrain)){
    while(!place_meeting(x+sign(horiz_spd),y,obj_terrain)){
        x += sign(horiz_spd);
    }
    horiz_spd =0;
}

if(place_meeting(x,y+vert_spd,obj_terrain)){
    while(!place_meeting(x,y+sign(vert_spd),obj_terrain)){
        y += sign(vert_spd);
    }
    vert_spd =0;
}   
x += horiz_spd;
y += vert_spd;
Here is the bullet create event also, although I do not believe it is the cause of the problem:
Code:
direction = point_direction(x,y,mouse_x,mouse_y);
direction = direction + random_range(-3,3);
speed = 15;
image_angle = direction;
Any help with this would be greatly appreciated :)
 
M

Mark M

Guest
I've figured out what my issue was. Generating the new bullet instance before the X and Y values were finalised seems to be the problem.
I moved the mouse_check to the bottom of the file and all is working well. ;)
 
M

Mark M

Guest
Hi all,

I'm re--opening this as it has become a problem again and this time I do not know why.

Whenever I am holding a keyboard, my mouse events are not triggered. The input checks are:

Code:
mouse_left = mouse_check_button(mb_left);
key_right = keyboard_check(ord("D"));
key_left = -keyboard_check(ord("A"));
key_up = keyboard_check_pressed(ord("W"));

//movement
move = key_right + key_left;
horiz_spd = move * player_speed;
if(vert_spd < 10){
    vert_spd += base_gravity;
}

if (place_meeting(x,y+1,obj)){
    vert_spd = key_up * -player_jump_power;
}

//collision checks
if(place_meeting(x+horiz_spd,y,obj_terrain)){
    while(!place_meeting(x+sign(horiz_spd),y,obj_terrain)){
        x += sign(horiz_spd);
    }
    horiz_spd =0;
}

if(place_meeting(x,y+vert_spd,obj_terrain)){
    while(!place_meeting(x,y+sign(vert_spd),obj_terrain)){
        y += sign(vert_spd);
    }
    vert_spd =0;
}  
x += horiz_spd;
y += vert_spd;

//fire check
if(mouse_left){
   instance_create_layer(x,y,"bullet_layer",obj_bullet);
}
Are there any general rules/best practices on how best to handle simultaneous keyboard/mouse input in gamemaker?

EDIT: I have added a debug message at the start of the step after the input is read. When a keyboard key is held, the mouse_check_button value never increases to 1 while clicking the mouse.

Thanks!
 
Last edited by a moderator:
M

Mark M

Guest
Back to solved o_O
I was testing on my laptop, the trackpad mouse' intelli-sense feature was supposed to be disabled so that I could use both keyboard and mouse input together but it wasn't. Using an external mouse resolves my problems.

:D
 
Top