• 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!

Dragging an object with the mouse

U

Uhfgood

Guest
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 :)
 
Q

Quackertree

Guest
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! :)
 
U

Uhfgood

Guest
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;
}
 
Q

Quackertree

Guest
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;
   }
}
 
U

Uhfgood

Guest
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
 
U

Uhfgood

Guest
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