Trigger change in opacity of polygon with mouse

OS Mac 10.13.6
PsychoPy version v2020.1.2
Standard Standalone? (y/n) yes?

What are you trying to achieve?:
Get a polygon object to disappear with a mouse click, but not end the trial.

Specifically, I am a newish user trying to create a two-alternative forced-choice recognition memory task in the Builder.

Target and distractor words appear to the left and right of the center of the screen, and the trial should end with a keypress (when the participant presses f to identify the stimulus on the left as the target or j to identify the stimulus on the right as the target.)

My goal:
The trick of my test phase is that, at the onset of each trial, one of the alternatives (target or distractor, left or right) should covered by a rectangle set at opacity = 1. If the participant wants to see the second alternative, then they have to click the rectangle covering it with their mouse.

A mouse click on the rectangle should trigger a change the opacity of the rectangle to 0, so that the participant can see the second word.

The participant does not have to click to box to see the second test item, and can end the trial with a keypress identifying the already-visible item, if they want. So the mouse click should be optional and not end the trial, but if the participant does click the rectangle, then the second word should show up.

What did you try to make it work?:
Current state:

Currently, I have the simple routine with words from a condition list that appear on the left and right, and trials that end with a keypress. That all works fine. Eventually I will need to set up counterbalancing, but am not quite there yet.

I am having trouble getting the rectangle to disappear with the mouse click. Based on this post: Clickable stimuli, draw rectangle - #4 by
I think that my issue is best solved with a code component, (like my current attempt below) but the rectangle never disappears.

Here is what is in my code component:

if mouse.isPressedIn(polygon1) :
opacity = 0

What specifically went wrong when you tried that?:
The rectangle doesn’t disappear. Trial ends with fine with keypress, but never reveals the second word. Photos below show settings for mouse and the components in the routine.

Quick tip/fix: try placing the code component at the very top of the component list.


Thanks for the reply, I do have a bad habit of putting things in the wrong place! I have moved the code component up, but the rectangle remains there, unchanged, despite clicking on it on each trial.

To me, it doesn’t look like the code that I have actually redefines polygon1 at all, it just says “if you click that polygon, then opacity=0,” but doesn’t say which object’s opacity is supposed to change.

if mouse.isPressedIn(polygon1) :
opacity = 0

How does psychopy know from that code, that I want the opacity of the polygon to change? How should that be rewritten to affect polgon1 on a trial-by-trial basis?

You can just change the opacity setting of polygon1 to $opacity and then it should work. Alternatively, you can write in the code component:

polygon1.Opacity = 0


Thanks again! That makes sense, and I can see how it should work, but for some reason, the rectangle still won’t disappear with a mouse click.

Currently, I have opacity = 1 in the Begin Routine tab of the code component, and have set polygon1 to have Opacity of $opacity. In the Each Frame component, I have tried both (separately):

if mouse.isPressedIn(polygon1) :
opacity = 0


if mouse.isPressedIn(polygon1) :
polygon1.Opacity = 0

but neither approach resets the opacity of the rectangle. My .psyexp file and conditions file are attached, in case it helps. It really seems like it should be working!

conditions.xlsx (8.7 KB) COARDP.psyexp (14.8 KB)

You just have to set the opacity of the polygon to change “every frame” instead of “every repeat”


Thanks so much! All of your help is very appreciated! It works now that I have the settings correct, based on your replies. :slight_smile:

You are welcome,

I’m warning you that if you plan to move your experiment online you’ll have troubles changing the opacity of the polygon online and I don’t think that the issue is resolved yet. So if this is the case i suggest you change the color of the polygon instead of the opacity (or to use an image instead and make it appear/disappear with a mouse click).


Thanks for the heads up–I do plan to move it online. Probably I should pilot this early stage work to see whether color or image work best. I really, really appreciate your advice!