psychopy.org | Reference | Downloads | Github

Change opacity, wait, end routine

Windows: Windows 10, 64 bit
PsychoPy version : 2021.1.4

Standard Standalone? (y/n) y

What are you trying to achieve?:
When clicking (with the mouse) on one of three stimuli, the opacity of the clicked stimulus (a jpg image) should change and after 0.5 seconds the routine should end. We want a delay of 0.5 seconds to allow participants to receive visual feedback on where they clicked (the opacity change). The three stimuli shown on the screen are called: Stim_1, Stim_2, Stim_3. We are working with code components in builder view for now, but will eventually want to transfer to Pavlovia.

There are two parts to this: (1) opacity change of stimulus and (2) waiting 0.5 s after a stimulus is clicked and then ending the routine. Both code components work separately but not together.

What did you try to make it work?:
(1) Here is the code for the opacity change:

In ‘Begin Routine’ tab:

timer = core.Clock()
Stim_1.opacity = 1
Stim_2.opacity = 1
Stim_3.opacity = 1

In ‘Each Frame’ tab:

if mouse.isPressedIn(Stim_1):
````Stim_1.opacity = 0.1
elif mouse.isPressedIn(Stim_2):
````Stim_2.opacity = 0.1
elif mouse.isPressedIn(Stim_3):
````Stim_3.opacity = 0.1

(2) This is the code for ending the routine, also entered in ‘Each Frame’:

if (Stim_1.opacity==0.1) or (Stim_2.opacity==0.1) or (Stim_3.opacity==0.1):
````timer.reset()
````while timer.getTime()<0.5:
``````continueRoutine=True
````else:
``````continueRoutine=False

What specifically went wrong when you tried that?:
If we run the first code component and fix the durations of all components in the routine (e.g. to 2s), we see the clicked stimulus change opacity. However, the change of opacity ‘runs into’ the next routine (i.e. that Stim is still grey in the next repeat of the loop). If the second code is added (now leaving the durations of all components empty), then we do not see the opacity change anymore, but it does go to the next routine after 0.5 s. Any tips or insights greatly appreciated!

I think the problem is that the second code will stall the frames loop - usually, the frames loop would progress and you’d get all your stimuli being drawn, the window flipping, checking for more responses, etc. but because of that while loop you’re instead just waiting for a timer, hence why the opacity change isn’t realised. Instead of a while loop, try using an if - as it’s in the Each Frame tab, it’ll be called repeatedly

Thanks @TParsons. Just swapping while for if unfortunately didn’t solve it, but your comment helped me think about it differently and fix the code so now it works! Here is the solution in case anyone else is interested in this functionality:

In Begin Routine:

timer = core.Clock()
timer.reset()
Stim_1.opacity = 1
Stim_2.opacity = 1
Stim_3.opacity = 1

In Each Frame:

if Stim_1.opacity==1 and Stim_2.opacity==1 and Stim_3.opacity==1:
    if mouse.isPressedIn(Stim_1):
        Stim_1.opacity = 0.1
    elif mouse.isPressedIn(Stim_2):
        Stim_2.opacity = 0.1
    elif mouse.isPressedIn(Stim_3):
        Stim_3.opacity = 0.1
else:
    if timer.getTime()<1.0:
        continueRoutine=True
    else:
        continueRoutine=False
1 Like