GML how to move objects like windows on your computor

C

Capptianm

Guest
you know the way you would move windows on your computer screen? how you hold left click on something and that thing moves with the mouse on the point the mouse clicked?

I want to move the mouse like this, and I thought I have a way to do this, but I got a few problems
Code:
//Left Pressed 
//set point
xxx = mouse_x - x
yyy = mouse_y - y

//Step 
//moving the object
if (mouse_check_button(mb_left))
{
    x = mouse_x - xxx
    y = mouse_y - yyy
}
when you introduce multiple instances of the same object, then you get an error, since variables xxx and yyy aren't actually set in objects that are not left-clicked.

Changing the step event to left down creates a new problem, as now the object only moves when the mouse is on the object, which is very annoying to deal with.

can anyone help me out here?
 
You need to keep a variable which tell which window you're moving. This code should go into a controll object, not in each instance of the window.
You probably also have to change the name of obj_window to the name you're using.

Create:
Code:
moving = noone
Step:
Code:
if mouse_check_button_pressed(mb_left) {
with obj_window {
if distance_to_point(mouse_x, mouse_y) == 0 {
other.moving = id
}
if moving != noone {
xxx = mouse_x - moving.x
yyy = mouse_y - moving.y
}
}

if moving != noone {
moving.x = mouse_x - xxx
moving.y = mouse_y - yyy
}

if mouse_check_button_released(mb_left) {
moving = noone
}
 
C

Capptianm

Guest
i w
You need to keep a variable which tell which window you're moving. This code should go into a controll object, not in each instance of the window.
You probably also have to change the name of obj_window to the name you're using.

Create:
Code:
moving = noone
Step:
Code:
if mouse_check_button_pressed(mb_left) {
with obj_window {
if distance_to_point(mouse_x, mouse_y) == 0 {
other.moving = id
}
if moving != noone {
xxx = mouse_x - moving.x
yyy = mouse_y - moving.y
}
}

if moving != noone {
moving.x = mouse_x - xxx
moving.y = mouse_y - yyy
}

if mouse_check_button_released(mb_left) {
moving = noone
}
After a bit of thinking I used my original method with the idea that you recommended with the variable that tells what object your moving
Code:
//Left Pressed
xxx = mouse_x - x
yyy = mouse_y - y

global.move = id
Code:
//Step
if (mouse_check_button(mb_left))
{
    with (global.move)
    {
        x = mouse_x - xxx
        y = mouse_y - yyy
    }
}
Code:
//Left Released
global.move = noone
 
Top