How Do You Make Sprites Draggable in Game Lab?

How do you make sprites draggable in game lab? I know how to do it in app lab but not game lab. I’m trying to make a game sorta like Papa’s Pizzeria.

I can try to make it in app lab but it’ll be harder?

Also, anyone know if its possible to make a game like Subway Surfers?

You can try using my UI5 library, which has built in dragging features.

If you don’t want to go through that process, you can have a variable (or attribute of a sprite).
When the mouse is pressed on the sprite, set the variable to true.
When the mouse is released, set the variable to false.
Each frame, if the variable is true, move the sprite’s position to the mouse’s position.


However, note that the center of the sprite will always be where the mouse is. When initially dragging the sprite, it will appear as if the sprite ‘teleported’ to the mouse. To fix this, we can add 2 more variables: relativeX, relativeY. To avoid ambiguity, the variable I mentioned above will be called ‘Held.’
  • When the mouse is pressed on the sprite, set Held to true. Also set relativeX to the difference between sprite.x and mouseX along with relativeY to the difference between sprite.y and mouseY.
  • When the mouse is released, set Held to false. relativeX and relativeY do not need to be changed.
  • Each frame, if Held is true, move the sprite’s position to the mouse’s position plus relativeX and relativeY.

You can check out the end result here.

1 Like

Here is an example. This may not be the only way to do it, but it seems like what most would do.

There are several functions to interact with the mouse. mousePressedOver will tell you if a sprite was clicked for example.

There are two parts to dragging a sprite. The first is sensing the click. In the example program above I have created two functions palletePress() and scorePress() both called in draw(). Basically, I check all the sprites to see if one was clicked using the function mousePressedOver(sprite). When clicked I save that sprite in a variable to remember we are dragging it. I don’t worry about sprites overlapping.

The second part is dragNote() called in draw(). What this does is it checks the variable and if there is a sprite, not null, then it moves that sprite to the mouse position. If dragNote() finds that there is a sprite being dragged but the left mouse button is no longer down it drops the sprite by setting the variable to null. It does that by calling, you guessed it, dropNote().