ruchir patwa
ruchir patwa

Reputation: 321

Dragging sprites in Scratch

How do I drag sprites during execution in Scratch?

Upvotes: 3

Views: 3229

Answers (6)

Xbox One
Xbox One

Reputation: 303

Sprites are by default draggable in Scratch 3.0. You can change that by using set drag mode [not draggable]

Upvotes: 0

Mr PizzaGuy
Mr PizzaGuy

Reputation: 460

You can use the <mouse down?> boolean, the touching [mouse pointer]? and the variables(mouse x) and (mouse y) to get the mouse's coordinates and to detect if the mouse is down. Here is how you can do it:

when green flag clicked                                               //when the green flag is clicked,
forever                                                                         //do this forever,
    if touching [mouse pointer]? AND mouse down?    //touching the mouse pointer? The mouse down?
        set x to (mouse x)                                               //set my x position to the mouse's x position.
        set y to (mouse y)                                               //set my y position to the mouse's y position.
    end if loop                                                              //if everything above is not true, don't go to mouse
end forever loop                                                        //repeat this process forever!

If you need more help with other things, you ca follow me if you want to @endermite334.

Upvotes: 0

Account_2
Account_2

Reputation: 61

If you would not mind if the sprite is still draggable when the script is not running then you press the i button at the top-right corner when the sprite is selected. Then, you press Can drag in player. However, this does not work for Scratch 3.0 and so you would need to use my other method, scripting.

when green flag clicked
forever
if <<mouse down?> and <touching [mouse-pointer]>>
go to [mouse-pointer]

Upvotes: 0

user6087607
user6087607

Reputation:

For a quick and simple route, all you have to do is click the info button of the sprite: Click here for image 1.
After that you should find the box that says: can drag in player and click that: Click here for image 2.
This is actually it. Now whenever somebody plays your game they can drag the sprite. You just have to let them know that it is possible since most project don't allow it.

Upvotes: 0

Scimonster
Scimonster

Reputation: 33409

This is covered on the Scratch Wiki.

boisvert's answer is technically correct, however, his script will always drag the sprite from it's center. Using a slightly more advanced script will drag from the spot it was picked up, more like the padlock:

when green flag clicked
forever
  if <<mouse down?> and <touching [mouse-pointer v]?>>
    repeat until <not <mouse down?>>
      set [offset x v] to ((x position) - (mouse x))
      set [offset y v] to ((y position) - (mouse y))
      go to x: ((mouse x) + (offset x)) y: ((mouse y) + (offset y))
    end
  else
    wait until <not <mouse down?>>
  end

(The wiki link above has this is visual blocks format.)

Upvotes: 8

boisvert
boisvert

Reputation: 3739

Click the padlock next to the sprite name. It will look open; then the sprite becomes draggable in the executable version.

Alternatively, you could program its dragged behaviour with a script:

if <mouse down>
  set x to (mouse x)
  set y to (mouse y)

it can be made more clever, to follow the mouse at an offset position, with a delay, snap to a position when dropped, highlight something as it passes over it... If you use a script your choices are limitless.

Upvotes: 5

Related Questions