• Hey Guest! Ever feel like entering a Game Jam, but the time limit is always too much pressure? We get it... You lead a hectic life and dedicating 3 whole days to make a game just doesn't work for you! So, why not enter the GMC SLOW JAM? Take your time! Kick back and make your game over 4 months! Interested? Then just click here!

Need help directing an object with the mouse

U

Undead Hero

Guest
Uh, sorry for the weird title, but I couldn't think of how to sum this up.

Basically, I'm making a game where an object travels to the right shmup style, but you use the mouse to direct it up and down. Here is the basic code:

Code:
x += spd;

if mouse_check_button(mb_left) {
    ObjectGrabbed = true;
} else if mouse_check_button_released(mb_left) {
    ObjectGrabbed = false;
}

if ObjectGrabbed = true {
    y = median(RoomTopY, mouse_y, RoomBottomY)
}
The variables are excessive just because a lot of other functions will be added later.
This works, and keeps the object within the play area, but obviously it also makes the object teleport to the mouse wherever you click. The goal is to make the object "float" to the mouse (y position only), then function the same as the previous grab code.

The problem is, I tried using this to accomplish that (in place of the previous grab code):

Code:
move_towards_point(x, median(RoomTopY, mouse_y, RoomBottomY), spd);
But this makes the object slide seemingly randomly to the right along the x axis in weird fits. And when it hits the top or bottom boundary, it slides smoothly to the right. How can I use code like this without affecting the x movement of the object? My limited GML knowledge hits a wall.
 
W

wagyu_so_gud

Guest
You can check to see what position mouse_y is at (when mb_left is held down) and determine if you should add or subtract the player's y value.

ex:

Code:
if mouse_check_button(mb_left)
{
    if obj_player.y > mouse_y //if obj_player is lower than the mouse_y val
    {
    y -= 5; //move obj 5 pixels up each step
    }
    else
    {
    y += 5; //otherwise, move 5 pixels down each step
    }

}
 
U

Undead Hero

Guest
Thanks man! I had to tweak it a bit because the object would vibrate wildly once it caught up with the mouse (at least with higher values). This is working so far, but fingers are still crossed:

Code:
if ObjectGrabbed = true {
    if (y-global.PlayerHandling) >= mouse_y {
        y -= global.PlayerHandling;
    } else if (y+global.PlayerHandling) <= mouse_y {
        y += global.PlayerHandling;
    } else {
        y = mouse_y;
    }
}
 

Fredrik

Member
If I'm not wrong you want to be able to click somewhere with the mouse making the player to walk there?
At first I thought you wanted the player to always stay at the mouse's position, well in the vertical line alteast, suggesting player.y = mouse_y; in step event. but oh well.
What you can do is set two variables in the player's Create Event:

var_mouse_x = player.x;
var_mouse_y = player.y;


this will just define the variables making them by default the player's x and y.
When pressing the mouse:

if mouse_check_button_pressed(mb_left) {var_mouse_x = mouse_x; var_mouse_y = mouse_y;}

So now every time you press LMB you'll redefine the var_mouse_y and var_mouse_x to be the mouse's x and y position.

In the player object's Step Event:
if player.x != var_mouse_x and player.y != var_mouse_y {move_towards_point(var_mouse_x,var_mouse_y,spd);}


I believe this will work but I'm not quite sure, the "move_towards_point" might be outdated and replaced in Studio.
 
Top