How do you make a polygon appear in response to a spacebar? (please help!)

OS (e.g. Win10): Win 10
PsychoPy version (e.g. 1.84.x): v2022.2.5

What are you trying to achieve?:

I want a polygon component to appear in response to the participant’s space bar.

In my loop, I have four following components

  1. Code component
  2. Polygon component (named polygon3)
  3. Key response component (space)
  4. Image component (duration: 5 seconds)

What did you try to make it work?:
I’ve played with the opacity of the polygon which worked well locally but does not work online.
For this, the code component had was

if event.getKeys(‘space’):
polygon3.opacity = 1 # Make the Polygon visible

under Begin Routine and Each Frame. This worked well locally but did not work online.

I’ve also tried a different code where I can actually make the polygon appear. For this, I had:

polygon3.setAutoDraw(False) in the Begin Routine

if key_resp.keys == ‘space’:
polygon.setAutoDraw(True) # Show the polygon when spacebar is pressed

under Each Frame tab. But did not work.

What specifically went wrong when you tried that?:
Include pasted full error message if possible. “That didn’t work” is not enough information.

If polygon3.setOpacity(1) doesn’t work you could change the position (start the polygon off screen and then polygon3.setPos([0,0]) to move it to the centre.

I only use setAutoDraw if the component itself has been created in code or in an earlier routine (e.g. for the instructions)

I also tend to use a flag to avoid repeating the code every Frame, e.g. Begin Routine spacePressed = False, Each Frame if key_resp.keys == ‘space’ and spacePressed == False: spacePressed = True

1 Like

Okay, so per your suggestion, I set it so that the position of the polygon starts off-screen. When participants press the spacebar the polygon now moves to the center. This works great. Unfortunately, it only works locally but not online.

For this, I set the ‘code component’ as follows (the name of my polygon is polygon)

Begin Experiment:
start_pos = [-1.5, 0] # Off-screen left
center_pos = [0, 0] # Center of the screen

Begin Routine:
polygon.setPos(start_pos)

Each Frame:
if event.getKeys([‘space’]):
polygon.setPos(center_pos).

Again, this works locally but does not work with online experiments.
I can’t seem to figure out what the issues are here.

Would it be the polygon properties that might be the issue? Here are some screenshots of how I set it up.

Or maybe spacebar properties?

This is the URL of my experiment.
https://run.pavlovia.org/hkim174/rt04052024/?__pilotToken=c81e728d9d4c2f636f067f89cc14862c&__oauthToken=7b6930a6851d14fd7b3b8d7968838b07e641b6371105253a5e20e29260e9d050

Some help would be greatly appreciated.

if spacebar.keys:
     polygon.setPos(center_pos)
1 Like

This worked! Thank you so much!

(For those encountering similar issues, make sure that you are using the “name” of the Keyboard component in your code, rather than using just a ‘space’)