GMS 2 Code doesn't work

A

aGreenCrystal

Guest
Hello.
I'm trying to make an instance move with the mouse,
but I want the camera to move with it, too.
I tried simply testing for mouse_x changes,
but when it moved the camera, the mouse_x changed,
and it moved the camera, ...
So I made it that the mouse gets put into the middle,
you move it, the position get saved, the mouse gets put into the middle,
and you move the difference between before mouse to middle and after mouse to middle.
Any idea why that doesn't work?

Code:

oldmx = mouse_x;
oldmy = mouse_y;
display_mouse_set(display_get_width()/2, display_get_height()/2);
x += oldmx - mouse_x;
y += oldmy - mouse_y;
 
The old values need to be old, but you're updating to the current before using them so you're just adding 0. Setting the old variables should happen after they've been used wherever necessary to ensure the next frame will still have the previous values. You should also update the coordinates before resetting the mouse, otherwise they'll still be identical.

Considering you're resetting the mouse position, you don't even need the old variables. Just get the mouse offset from the middle of the screen.
 
try instead:

x += window_mouse_get_x() - window_get_width()/2;
y += window_mouse_get_y() - window_get_height()/2;
window_mouse_set(window_get_width()/2,window_get_height()/2);

and you'll probably want to set the mouse to the center of the screen at the start of the game as well, to keep the view from jumping at the very start
 
Top