Dragging an object with the mouse

Discussion in 'Programming' started by Uhfgood, Jan 4, 2017.

  1. Uhfgood

    Uhfgood Member

    Joined:
    Sep 26, 2016
    Posts:
    87
    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 :)
     
  2. Quackertree

    Quackertree Member

    Joined:
    Jun 20, 2016
    Posts:
    64
    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! :)
     
  3. Uhfgood

    Uhfgood Member

    Joined:
    Sep 26, 2016
    Posts:
    87
    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 likes this.
  4. Quackertree

    Quackertree Member

    Joined:
    Jun 20, 2016
    Posts:
    64
    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. :)
     
  5. Perseus

    Perseus Not Ragarnak Forum Staff Moderator

    Joined:
    Dec 29, 2016
    Posts:
    953
    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;
       }
    }
    
     
  6. Uhfgood

    Uhfgood Member

    Joined:
    Sep 26, 2016
    Posts:
    87
    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
     
    Quackertree likes this.
  7. Uhfgood

    Uhfgood Member

    Joined:
    Sep 26, 2016
    Posts:
    87
    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?
     

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice