psychopy.org | Reference | Downloads | Github

Signaling when an image is clicked

Hi, (changing a previous too complicate post)

I have created an experiment in the builder with a code component. In short, a sound is played, and participants then need to select an image by clicking on it with the mouse. The code component makes it possible to register which image was selected and also to signal to the participant which image one has clicked on through changing the opacity of the clicked image. The opacity is restored to normal again. I did this by using win.flip() between the opacity specifications, however, I was just told (thank you Michael) that win.flip() is not supposed to be used in the builder and it might be interfering with the registering of images (sometimes, not all clicks get registered!). Does anyone have a suggestion for how I can substitute win.flip() below with something else and still get the effect that when an image is clicked, it changes opacity for a fraction of a second? The code is below.

for stimulus1 in [ uh, oh, ah, au, u, o, a]:
    if mouse.isPressedIn(stimulus1):
        continueRoutine = False
        thisExp.addData('clicked', stimulus1.name)
        stimulus1.opacity = 0.2
        win.flip()
        stimulus1.opacity = 1 
    if continueRoutine:  
        event.clearEvents()
  • You need to format your code properly so we can see the indenting. But at a quick check, it looks like you create a clock when the mouse is clicked, via RT = core.Clock(), so the RT would always be zero?
  • You are calling win.flip(). Never do that in a Builder experiment. Builder takes care of the drawing for you: this will completely muck up its drawing and event loop cycle. Not sure where you got this code, but it is not designed to work with Builder.
  • Please be careful with your terminology: you seem to be using “frame” to refer to two different things.

Hi Michael, thank you for answering. I realized I am actually asking many things at once so changed the current post and will create a new post just on reaction times. Regarding win.flip(), the experiment works with it because the opacity of the clicked images changes for just a fraction of a second to signal that it has been clicked. If I’m not suppose to use it in the builder, do you know what I could replace it with here?

You can still change the opacity, just don’t call win.flip(). Code in this tab of the code component runs once per win.flip() that Builder calls. If you insert another one here, you will interrupt the timing of every other aspect of Builder: it’s expecting the code in this tab to be complete within one screen refresh, but you’ve just inserted a whole new pause for another refresh. So on a 60 Hz screen, this code will take 33.4 ms to execute, rather than 16.7 ms.

So what you need to do is add some logic so that you can flip the opacity across successive runs of the code in this tab. e.g. something like:

Begin experiment tab:

opacity_low = False

Every frame tab:

# check if the stimulus opacity was changed on the last refresh. If so, set it 
# back to 1.0 and then end the routine after it is shown again:
if opacity_low:
    stimulus1.opacity = 1 # make it fully visible again
    opacity_low = False # reset for next trial
    continueRoutine = False # end trial after showing stimulus on next frame.
else: # check for mouse clicks:
    for stimulus1 in [ uh, oh, ah, au, u, o, a]:
        if mouse.isPressedIn(stimulus1):
            thisExp.addData('clicked', stimulus1.name)
            stimulus1.opacity = 0.2
            opacity_low = True

So now, on a mouse press, the stimulus opacity is set to 0.2 and the routine continues. On the next frame, the stimulus is set back to 1.0 and the routine will end after that.

This is the same arrangement you had before, but now you are working within Builder’s drawing loop, rather than disrupting it by inserting your own win.flip()

Thank you so so much Michael! This works perfectly. I only needed to add stimulus1.opacity = 1 right below for stimulus1 in [ uh, oh, ah, au, u, o, a]: because if not, the images would remain opaque until they were clicked.

Thank you again!