• Hey! Guest! The 36th GMC Jam will take place between February 27th, 12:00 UTC - March 2nd, 12:00 UTC. Why not join in! Click here to find out more!

Dragging an object with the mouse

Uhfgood

Member
I basically want to drag the paddle in my breakout clone with the mouse. The way I have it now, it will jump or move to the position where the center of the paddle is at the mouse position.

It means if you click on the left or right edge it will move to where it's in the middle of the paddle. I want to move the paddle like a scroll bar in Windows. You click on it, and drag it to what position you want.

Anyone tell me how to do that? hmm I guess I need a difference in mouse position :)
 

Quackertree

Member
You detect if the mouse button is pressed down, then, indeed, move it by the difference (delta) mouse position from the previous frame.

Something like:

Code:
if(mouse_check_button(mb_left))
{
var dx = (mouse_x - mouse_xprev);
var dy = (mouse_y - mouse_yprev);

x += dx;
y += dy;

mouse_xprev = mouse_x;
mouse_yprev = mouse_y;
}
The above code is just sort-of, so you might need to tweak some things a little bit.

Good luck! :)
 

Uhfgood

Member
Thanks for the prompt reply, I figured it out. Just what you said...
in a step event.
(created the variables is_dragging and prev_mouse_x in a create event)

Code:
if( is_dragging == false )
{
    if( mouse_check_button( mb_left ) )
    {
        if( collision_point( mouse_x, mouse_y, self, false, false ) )
        {
            is_dragging = true;
        }
    }
}

if( is_dragging == true )
{
    var mouse_travel = mouse_x - prev_mouse_x;
    x += mouse_travel;
    prev_mouse_x = mouse_x;
}

if( !mouse_check_button( mb_left ) )
{
    is_dragging = false;
    speed = 0;
    prev_mouse_x = mouse_x;
}
 

Quackertree

Member
If I may give a small pro tip: There's a thing called and (written as &&), which will check two cases within one if. This means you won't have to if-nest (aka place many if's under many if's, like in the first lines of your code).

Example:

Code:
if(x == 0)
{
if(y == 2)
 {
 //Do stuff
 }
}
Becomes:

Code:
if(x == 0 && y == 2)
{
//Do stuff
}
You can (theoretically) infinitely chain these. :)
 

Perseus

Not Medusa
Forum Staff
Moderator
Create:
Code:
mx = -1;
my = -1;
drag = false;
Step:
Code:
if (!drag) {
   if (mouse_check_button_pressed(mb_left) && position_meeting(mouse_x, mouse_y, id)) {
      drag = true;
      mx = x - mouse_x;
      my = y - mouse_y;
   }
}
else {
   x = mouse_x + mx;
   y = mouse_y + my;
   if (!mouse_check_button(mb_left)) {
      drag = false;
   }
}
 

Uhfgood

Member
Yeah I know I can use && -- I just chose to nest the if statements -- makes it clearer to me... however... making it shorter I guess can have it's points as well
 

Uhfgood

Member
The dragging of the paddle on android seems sluggish for some reason -- maybe because it's a low res game and it's scaling up to the phone display? On windows it's nice and snappy, and on html5 it's mostly snappy (except maybe a little slower than the windows version) -- anyone have any ideas on how to make it quick?
 
Top