Home‎ > ‎

Drag and Drop - (Grab command)

Drag and Drop Objects - (Grab command) (drag-n-drop code)

The Grab command (with a mouseDown) grabs and object and holds an object until the mouse button is released

An example of its use is:

Put 3 buttons on the right side of the screen and name them "red", "blue" and "green". Then make them each the color of their name.
Put a button on the left side and name it "box", color it red and add the following code to it

    on mouseDown

       grab me

    end mouseDown


    on mouseUp

       if intersect(button "box",button "red") then

            answer "Good job!"

      end if

      if intersect(button "box",button "blue") then

            set loc of button "box" to 50,50

      end if

      if intersect(button "box",button "green") then

           set loc of button "box" to 50,50
      end if

    end mouseUp


Now when you drag the box, it will give a message "Good Job" if it matches the color of the box you drop it on. Otherwise, it will send the box back to the start.


This technique is good for educational games (like match, etc) and games like Sheep Herder



The Set Command

Using the Set command, you can also do it this way by adding this code to the object that you want to move:


    on mouseMove
       if the mouse is down then  set  the loc of me to  the mouseLoc
    end mouseMove


 


NEXTChanging Properties in Code

Comments