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