Discuss Scratch

johnwaweru
New Scratcher
2 posts

How to swap positions of two sprites through the use of drag and drop

I'm developing a simple puzzle game that has 25 equal pieces covering the entire stage. I have so far been able to position the sprites where I want them to be located and can easily reset the default position by clicking the green flag. Positioning is achieved by defining the x and y values of each sprite. My problem arises when I try to swap two sprites by dragging and dropping one sprite on top of the other. I tried to achieve this by triggering an event when one sprite is touching another sprite and swapping their x and y values.
This doesn't seem to work very well. How best can I achieve this drag, drop and swap effect?
stickfiregames
Scratcher
1000+ posts

How to swap positions of two sprites through the use of drag and drop

This script should work if the pieces are aligned to a grid:

foreveriftouchingmouse-pointer?andmousedown?setlast xtoxpositionsetlast ytoypositionrepeatuntilnotmousedown?gotomouse-pointergotox:roundxposition/50*50y:roundyposition/50*50 this will 'snap' the pieces to a gridsetnew xtoxpositionsetnew ytoypositionbroadcastswapwhenIreceiveswapifxposition=newxandyposition=newygotox:lastxy:lasty
johnwaweru
New Scratcher
2 posts

How to swap positions of two sprites through the use of drag and drop

Thank you. This looks good. Let me give it a try and see how it functions.
spacebabexd
New Scratcher
1 post

How to swap positions of two sprites through the use of drag and drop

Is it possible to swap multiple sprites based on a randomized pattern.
bustedthumbs
Scratcher
100+ posts

How to swap positions of two sprites through the use of drag and drop

This was my first ever Scratch project, so it's not great, but it lays out 25 random sprites in a 5x5 grid at the start and may help you:

https://scratch.mit.edu/projects/91888877/

Powered by DjangoBB