psychopy.org | Reference | Downloads | Github

Moving objects on Pavlovia

URL of experiment:

Description of the problem: I’m new to PsychoPy and Pavlovia. I have 2 images shown on a display. Now I want the participants to move the objects with the arrow keys on the keyboard.
In Python I created 2 objects and specified the position with (C, -0.3) or (E, -0,3). In the custom code I wrote in the Begin Experiment tab
kb = keyboard.Keyboard()
kb.getKeys(‘right’, ‘left’, ‘backspace’)
C=0.025
E=-0.025

and in the Each Frame tab:

if event.getKeys(‘left’):
E-=0.01

if event.getKeys(‘right’):
E+=0.01

This works perfectly and I can move the one object to the left or the right.
But when I upload it to Pavlovia it doesn’t work. How can I translate this code to Javascript so that it also works on Pavlovia?

Thank you very much for your help in advance!

In the Each Frame tab add:

if event.getKeys(‘left’):
     E-=0.01
     objectE.setPos([E,-.3])
if event.getKeys(‘right’):
     E+=0.01
     objectE.setPos([E,-.3])

Each Frame changes don’t seem to work in components so I just set the position to constant in the component and change it in the code.

Best wishes,

Wakefield

There are several reasons why that is failing.

  1. I said ObjectE in my code because I didn’t know the name of the polygon. It looks like it is polygon_4

  2. I’m not sure about your definition of keyboard in Begin Experiment. I think you should delete it entirely.

  3. You need event=psychoJS.eventManager; in code_JS as per my crib sheet for event.getKeys to work.

  4. I’ve not seen event.getKeys used with the names of the keys in the bracket. I normally use something like the following:

    keys = event.getKeys()
    if len(keys) > 0:
        if 'left' in keys:

You don’t need to worry about specifying which keys are allowed because your code component will only react to allowed keys.

  1. There’s no point having the polygon update every frame since it’s being updated in the code component.

You need two code components. code_JS set to JS and then your normal one set to Auto. My previous message contained Python script that I know the auto translate will translate correctly.

space not spacebar

In what way is it not working?

You need to add code for continueRoutine=False if space is pressed and then setPos to the start positions in Begin Routine

Please could you show your Begin Routine and Each Frame code tabs?

In Begin Routine you should put E=0 (or whatever) as well

In Each Frame you appear to have a lot of additional JS code which doesn’t look like it came from the auto translate.

Also, you don’t need to repeat the len check. The Python for else if is elif

Check that the polygon position is constant or each repeat not Each Frame in the component

The position was constant. Now I tried it also with each repeat, but it still doesn’t work

Are you getting any errors or relevant warnings in the console?

No everything works well. It’s just that I can’t move the polygones anymore. So nothing happens when I press one of the arrow keys

It may be that Online you have to call left and right ArrowLeft and ArrowRight

Please could you try this out and let me know ?

In that case please could you share your experiment with me or message me the .psyexp (and Excel) files so I can take a look myself?

I have fixed the following:

  1. Added $ to the locations of polygons 3 and 4.
  2. Move the code components above the polygons
  3. Moved the sound code to code_JS and removed the event definition from the Python code.
  4. Added an ISI in the outer loop
  5. Space now ends the inner loop instead of ending a trial that only lasts one frame anyway. To do this I switched the names of the loops so the inner loop is called trials.
  6. Removed all setPos and changed Polygons 3 and 4 to update every routine since each trial only lasts 1 frame anyway
  7. Stopped the polygons swapping over
  8. Save the values of C and E each frame.
  9. Stopped space ending the loop if trials.thisN < 26

I’ve only tested it offline. I’ll send you back the file so you can see if it’s working better now.

Thank you so much! Now it works!