Discuss Scratch
- Discussion Forums
- » Help with Scripts
- » Making a click and drag script?
- newguyman55
-
Scratcher
12 posts
Making a click and drag script?
I am trying to make a click and drag script in my project. More specifically, I am trying to recreate the Player coin in Smash Bros where you drag and drop the coin to select a player. How do I do that?
- drmcw
-
Scratcher
1000+ posts
Making a click and drag script?
when this sprite clicked
repeat until <not <mouse down?>>
go to [mouse-pointer v]
end
- zacadoole1
-
Scratcher
100+ posts
Making a click and drag script?
Alternatively, just go into the sprite info/options panel thing (where you type the name of the sprite), and check the box that says “Can drag in player.”
- gtoal
-
Scratcher
1000+ posts
Making a click and drag script?
I am trying to make a click and drag script in my project. More specifically, I am trying to recreate the Player coin in Smash Bros where you drag and drop the coin to select a player. How do I do that?
What people often forget is that the drag & drop should be based on the center of the object being dragged, rather than the mouse position. If you click on the edge of an object and drag it, you need to position the dragged object offset by the *initial* distance from the mouse to the object's origin. Then when deciding where to drop it, use the center of the object rather than the final mouse position. It will look much more natural this way, and you never get that effect where the initial click makes the object jump to the mouse location. Remember also that you can make an object look like it has been raised from the playing surface by moving it slightly (eg up and right), and if you want to get really fancy, you can ghost it and cast a shadow (by cloning).
when green flag clicked
forever
if <<touching [mouse-pointer v]?> and <mouse down?>> then
set [offset x v] to ((x position) - (mouse x))
set [offset y v] to ((y position) - (mouse y))
create clone of [myself v]
repeat until <not <mouse down?>>
go to x:(((mouse x) + (offset x)) + (5)) y:(((mouse y) + (offset y)) + (3))
end
go to x:((mouse x) + (offset x)) y:((mouse y) + (offset y))
end
end
when I start as a clone
set [ghost v] effect to (75)
repeat until <not <mouse down?>>
go to x:((mouse x) + (offset x)) y:((mouse y) + (offset y))
end
delete this clone
There's a slightly extended version of the above which also handles dropping on a grid at https://scratch.mit.edu/projects/58544578/
and if you want to get really fancy, here's a more complex version that also handles single- and double-click detection: https://scratch.mit.edu/projects/58567194/
Last edited by gtoal (April 23, 2015 20:44:00)
- bugsy007
-
Scratcher
2 posts
Making a click and drag script?
awesome i can put blocks look
when this sprite clicked
repeat until <><not <>><mouse down?>
go to [ v] there's a problem
Last edited by bugsy007 (May 9, 2019 01:45:22)
- bugsy007
-
Scratcher
2 posts
Making a click and drag script?
when green flag clickedthat did not work
say [uhhhhhhhhhhh ok?]
- kritav
-
Scratcher
100+ posts
Making a click and drag script?
Please do not blockspam and necropost.when green flag clickedthat did not work
say [uhhhhhhhhhhh ok?]
- Discussion Forums
- » Help with Scripts
-
» Making a click and drag script?
