# Drag & drop feature in builder

**URL:** <https://discourse.psychopy.org/t/drag-drop-feature-in-builder/2046>\
**Category:** Builder\
**Tags:** mouse\
**Created:** [March 3, 2017, 8:49am UTC](https://discourse.psychopy.org/t/drag-drop-feature-in-builder/2046 "2017-03-03T08:49:08Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![JosephA](https://avatars.discourse-cdn.com/v4/letter/j/f05b48/32.png) [@JosephA](https://discourse.psychopy.org/u/JosephA)\
**Post date:** [March 3, 2017, 8:49am UTC](https://discourse.psychopy.org/t/drag-drop-feature-in-builder/2046/1 "2017-03-03T08:49:08Z")

</div>

Dear all,

I’m building an experiment in which participants need to choose the right stimulus then drag and drop it in a polygon in the center of the screen. Now the program is up & running (for now they just click on the stimulus and it appears in the polygon) however I don’t know how to code in the builder the “drag & drop” part.  
I found this Topic from 2014:  
[https://groups.google.com/forum/#!topic/psychopy-users/sroiwM0kxtM](https://groups.google.com/forum/#!topic/psychopy-users/sroiwM0kxtM)

In which @Michael suggests the following code:

```auto
elif (mouse3):
    if grating.contains(myMouse):
        grating.setPos(myMouse.getPos())

```

However, the users asking the question were using code not the builder. I tried it in the “each frame” tab in the builder but it didn’t work (either an error message saying elif (mouseS1): \<-- invalid syntax, or the mouse stops working altogether).  
Is there a different code for the builder? Or am I simply using it in the wrong place?

Thanks in advance.

Joseph

**OS** : Windows 7  
**PsychoPy version** : 1.84.2

---

<div class="post-metadata">

**Author:** ![Oli](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.psychopy.org/oli/32/520_2.png) [@Oli](https://discourse.psychopy.org/u/Oli)\
**Post date:** [March 3, 2017, 4:24pm UTC](https://discourse.psychopy.org/t/drag-drop-feature-in-builder/2046/2 "2017-03-03T16:24:48Z")

</div>

Hi - in builder you are limited to updating the position every frame and this leads to quite a clunky response - but give it a try and see what you think:

Set up a code component

```auto
#Begin experiment
dynPos = (0,0) # dynamic position variable

#Every Frame
while mouse.isPressedIn(polygon):
    dynPos = mouse.getPos()

```

Then you’d just set your image position to set every frame and have $dynPos as the value.

I think the optimal component order would be:  
Code Component  
Mouse  
Image - that way there won’t be a frame delay between variable setting and response.

BW

Oli

---

<div class="post-metadata">

**Author:** ![JosephA](https://avatars.discourse-cdn.com/v4/letter/j/f05b48/32.png) [@JosephA](https://discourse.psychopy.org/u/JosephA)\
**Post date:** [March 5, 2017, 11:48am UTC](https://discourse.psychopy.org/t/drag-drop-feature-in-builder/2046/3 "2017-03-05T11:48:35Z")

</div>

Hi Oliver,

Thanks for your response.  
I’ll try it today, however I was just wondering, I have 6 images displayed on the screen, hence, the position of the correct image is random.  
Does the $dynPos still apply for all their 6 positions or do I have to edit my excel sheet or insert another code?  
If it significantly complicates the coding from the builder, how can I know where do I insert the original code (given by Michael) in the script without messing up my whole program?

Thanks in advance.

Cordially,  
Joseph

---

<div class="post-metadata">

**Author:** ![Oli](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.psychopy.org/oli/32/520_2.png) [@Oli](https://discourse.psychopy.org/u/Oli)\
**Post date:** [March 6, 2017, 11:58am UTC](https://discourse.psychopy.org/t/drag-drop-feature-in-builder/2046/4 "2017-03-06T11:58:19Z")

</div>

No - if you use dynPos on all stimuli they will all move simultaneously. You’d need to have a variable for each (dynPos1, dynPos2 etc). Then the easiest way would be to just have 6 while statements - it’ll look messy but should work.

---

<div class="post-metadata">

**Author:** ![JosephA](https://avatars.discourse-cdn.com/v4/letter/j/f05b48/32.png) [@JosephA](https://discourse.psychopy.org/u/JosephA)\
**Post date:** [March 7, 2017, 1:43pm UTC](https://discourse.psychopy.org/t/drag-drop-feature-in-builder/2046/5 "2017-03-07T13:43:40Z")

</div>

Hi Oliver,

I have a slight issue. My protocol is divided into 4 routines in each Block: choice, waiting, rating, rating2  
I tried the code in the first two routines (choice & waiting) on 4 images. Indeed it’s a clunky response.  
Problem is, it works in only one of the routines (waiting routine instead of the choice routine) & I can’t seem to figure out why.  
The only difference is, in the waiting routine there is no mouse response so no mouse in the protocol. But the drag & drop worked there anyway!

Given that I want this component in the choice routine it’s useless to me if it only works in the waiting routine.  
Hence, I removed the code from the waiting routine. Then I tried removing the mouse response code component I already had in the choice routine, but the mouse stopped working altogether. I tried removing the mouse, changing the names of the $dynPos in case both codes were colliding…  
In the code I used “cadre” instead of Polygon because that’s what its called in my routine. I tried polygon also but it didn’t work.  
Please find attached screenshots of my flow and how my code is looking at the moment. Maybe you’ll spot something I missed.  
Thanks in advance.

Cordially,  
Joseph

EDIT clarification: In the choice routine when I click on image it appears directly in the middle of the screen instead of being able to drag & drop. Once that’s done & the waiting routine starts & I can move the image around.

 ![](https://us1.discourse-cdn.com/flex002/uploads/psychopy/original/2X/9/92db57b66afcb5b75c602b1a9e5e70621ab82ede.JPG)  
 ![](https://us1.discourse-cdn.com/flex002/uploads/psychopy/original/2X/3/39f1ada73c564e4ad597e12bf9524176fbe23d10.JPG)  
 ![](https://us1.discourse-cdn.com/flex002/uploads/psychopy/original/2X/2/286dbb724f6cf0c1540dabb9424e746e8e87364c.JPG) ![](https://us1.discourse-cdn.com/flex002/uploads/psychopy/original/2X/9/9624b2f44f1130bd1ee0c09e24814993803b06e5.JPG) ![](https://us1.discourse-cdn.com/flex002/uploads/psychopy/original/2X/6/6937e8f377e344da8899e7ec5f2216461eaa0acb.JPG)
