| Reference | Downloads | Github

Drag & drop feature in builder

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:!topic/psychopy-users/sroiwM0kxtM

In which @Michael suggests the following code:

elif (mouse3):
    if grating.contains(myMouse):

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.


OS : Windows 7
PsychoPy version: 1.84.2

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

#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
Image - that way there won’t be a frame delay between variable setting and response.



1 Like

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.


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.

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.


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.