1
\$\begingroup\$

I am trying to figure out how to drag a sprite. I know when the mouse is being hovered, pressed, or released over a sprite. I don't know how to tell if the mouse is being pressed and moved at the same time. Every time I try to use event.type, I only get one mouse event back (SDL_MOUSEMOTION, SDL_MOUSEBUTTONUP, or SDL_MOUSEBUTTONDOWN). I tried doing something like (pseudo-code):

if(event.type == SDL_MOUSEMOTION && event.type == SDL_MOUSEBUTTONDOWN && mouse_in_sprite && left_button_pressed) {
    /* Never goes in here */
}

However, it never goes inside that if statement because the event.type is never more than one of the above mentioned every frame (unless I'm doing it wrong).

In the picture below, I am trying to make it so the scrollbar on the right moves up and down like a normal scrollbar would when pressing the left mouse button on the sprite and moving the mouse up or down.

enter image description here

Unfortunately, I don't know how to proceed further than getting the mouse position when the mouse is pressed on the sprite (pseudo-code):

if (mousePressed(SDL_BUTTON_LEFT)) {
   currentMousePosition = getMousePosition();

   /* Do something with currentMousePosition.x, currentMousePosition.y */
}

Any help would be appreciated! Thank you!

\$\endgroup\$

1 Answer 1

3
\$\begingroup\$

"Pressed" and "Released" are usually treated as events, rather than states. (ie. "Became Pressed" rather than "Is Still Pressed")

So processing a drag can be as simple as:

  1. When the Pressed event fires for a given sprite, set a "isBeingDragged" variable to true (or set the "draggedObject" to point at that sprite, depending on how you're implementing this)

  2. When a Move event fires, if "isBeingDragged" is true (or "draggedObject" is not null), move the sprite.

  3. When the Released event fires, set "isBeingDragged" back to false (or "draggedObject" to null)

\$\endgroup\$
0

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .