# How Do You Make Sprites Draggable in Game Lab?

**URL:** <https://forum.code.org/t/how-do-you-make-sprites-draggable-in-game-lab/33727>\
**Category:** Coding and Debugging Help\
**Created:** [November 13, 2020, 11:08pm UTC](https://forum.code.org/t/how-do-you-make-sprites-draggable-in-game-lab/33727 "2020-11-13T23:08:54Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![hana.bananananananan](https://avatars.discourse-cdn.com/v4/letter/h/c0e974/32.png) [@hana.bananananananan](https://forum.code.org/u/hana.bananananananan)\
**Post date:** [November 13, 2020, 11:08pm UTC](https://forum.code.org/t/how-do-you-make-sprites-draggable-in-game-lab/33727/1 "2020-11-13T23:08:54Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![infinitestasis](https://sea2.discourse-cdn.com/flex016/user_avatar/forum.code.org/infinitestasis/32/5047_2.png) [@infinitestasis](https://forum.code.org/u/infinitestasis)\
**Post date:** [November 13, 2020, 11:56pm UTC](https://forum.code.org/t/how-do-you-make-sprites-draggable-in-game-lab/33727/2 "2020-11-13T23:56:57Z")

</div>

You can try using my [UI5](https://studio.code.org/projects/gamelab/WECf_x6V4H7MqleQ8BS9bHgAZxlHf1Oft_1TtOtd56c/view) 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.

> **[Gyazo](https://gyazo.com/7a7618aafe62ca1b78643c497113f08a)**

  
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](https://studio.code.org/projects/gamelab/Mxj5df_Jr548Mh0cSSygLwxOFjQp-u_1H6WWRXOaZIk/view).

---

<div class="post-metadata">

**Author:** ![jdonwells](https://sea2.discourse-cdn.com/flex016/user_avatar/forum.code.org/jdonwells/32/7900_2.png) [@jdonwells](https://forum.code.org/u/jdonwells)\
**Post date:** [November 14, 2020, 3:17pm UTC](https://forum.code.org/t/how-do-you-make-sprites-draggable-in-game-lab/33727/3 "2020-11-14T15:17:57Z")

</div>

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

> **[Check out what I made](https://studio.code.org/projects/gamelab/zdXqawUogZMD0FOgLIzGC2XrQnbcnxeVbiontF_gzaI)**
>
> I wrote the code myself with Code.org

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()`.

---

<div class="post-metadata">

**Author:** ![pluto](https://sea2.discourse-cdn.com/flex016/user_avatar/forum.code.org/pluto/32/9141_2.png) [@pluto](https://forum.code.org/u/pluto)\
**Post date:** [September 13, 2022, 12:53am UTC](https://forum.code.org/t/how-do-you-make-sprites-draggable-in-game-lab/33727/4 "2022-09-13T00:53:08Z")

</div>

Sorry if this is a dead topic, but I’m wondering how to do it with sprites that have been added to a group when you press down a certain key. I’m basically asking how you basically drag an individual sprite from the same variable, but not all of them.

(sorry I’m really bad at wording things)

---

<div class="post-metadata">

**Author:** ![mwood](https://sea2.discourse-cdn.com/flex016/user_avatar/forum.code.org/mwood/32/4941_2.png) [@mwood](https://forum.code.org/u/mwood)\
**Post date:** [September 13, 2022, 1:38pm UTC](https://forum.code.org/t/how-do-you-make-sprites-draggable-in-game-lab/33727/5 "2022-09-13T13:38:40Z")

</div>

@pluto,

If you know the index # of the sprite, you can create actions that happen to only that sprite. Basically, every sprite that you add to a group is given an index number based on the order that it was inserted, starting with index number zero.

Here’s a link to some documentation where they discuss this along with a sample program that targets individual sprites.

[https://studio.code.org/docs/ide/gamelab/expressions/get](https://studio.code.org/docs/ide/gamelab/expressions/get)

Good luck!

Mike

---

<div class="post-metadata">

**Author:** ![pluto](https://sea2.discourse-cdn.com/flex016/user_avatar/forum.code.org/pluto/32/9141_2.png) [@pluto](https://forum.code.org/u/pluto)\
**Post date:** [September 13, 2022, 10:07pm UTC](https://forum.code.org/t/how-do-you-make-sprites-draggable-in-game-lab/33727/6 "2022-09-13T22:07:16Z")

</div>

I’m saying that when you press a certain key a new sprite gets added to the group, which means that it can be any sprite index number

I want to make it so that the sprites from the group can all move, but not the whole entire group will move.

---

<div class="post-metadata">

**Author:** ![mwood](https://sea2.discourse-cdn.com/flex016/user_avatar/forum.code.org/mwood/32/4941_2.png) [@mwood](https://forum.code.org/u/mwood)\
**Post date:** [September 13, 2022, 10:34pm UTC](https://forum.code.org/t/how-do-you-make-sprites-draggable-in-game-lab/33727/7 "2022-09-13T22:34:11Z")

</div>

In that case, there needs to be a way to know which sprite you will be selecting. Maybe you can get the index when you create it?

Mike

---

<div class="post-metadata">

**Author:** ![pluto](https://sea2.discourse-cdn.com/flex016/user_avatar/forum.code.org/pluto/32/9141_2.png) [@pluto](https://forum.code.org/u/pluto)\
**Post date:** [September 13, 2022, 11:12pm UTC](https://forum.code.org/t/how-do-you-make-sprites-draggable-in-game-lab/33727/8 "2022-09-13T23:12:16Z")

</div>

Sorry if you do not know what I mean, the same sprite variable basically gets duplicated.

---

<div class="post-metadata">

**Author:** ![mwood](https://sea2.discourse-cdn.com/flex016/user_avatar/forum.code.org/mwood/32/4941_2.png) [@mwood](https://forum.code.org/u/mwood)\
**Post date:** [September 13, 2022, 11:58pm UTC](https://forum.code.org/t/how-do-you-make-sprites-draggable-in-game-lab/33727/9 "2022-09-13T23:58:15Z")

</div>

I have seen sprites duplicated before, like when you create a Sprite in a draw loop. In that case, my experience has been that the results can be erratic because having multiple sprites with the same name causes issues that are next to impossible to resolve.

However, if you are adding sprites to a group, even if theybhave the same costume, they should have an index you can find, but it’s a little difficult to troubleshoot a non-specific situation.

Sorry if I’m not quite able to grasp your situation.

There are some experienced users here on the forum who have made much more advanced games than i have, so perhaps one or more of them will see this and chime in.

Mike

---

<div class="post-metadata">

**Author:** ![pluto](https://sea2.discourse-cdn.com/flex016/user_avatar/forum.code.org/pluto/32/9141_2.png) [@pluto](https://forum.code.org/u/pluto)\
**Post date:** [September 14, 2022, 12:16am UTC](https://forum.code.org/t/how-do-you-make-sprites-draggable-in-game-lab/33727/10 "2022-09-14T00:16:24Z")

</div>

It’s fine. Thanks though!
