psychopy.org | Reference | Downloads | Github

Rotate a line when mouse is clicked on a stimuli

Hello everybody,

I am trying to setup a task, where participants have to rotate lines drawn in circles for 360°. One mouse click rotates a line for 90°. When finished with one line, they should move on to the next one.

I defined a mouse component in the builder and set the four different circles as clickable stimuli. To rotate the lines, I inserted the following code component, which works fine offline but produces odd outcomes online. Online, when any circle is clicked, every line rotates for 90°.

Begin Experiment (PY):

line1.ori = 270
line2.ori = 270
line3.ori = 270
line4.ori = 270

clickable = [[peg1,line1], [peg2, line2], [peg3, line3], [peg4, line4]]

Begin Experiment (JS):

line1.ori = 270;
line2.ori = 270;
line3.ori = 270;
line4.ori = 270;
clickable = [[peg1, line1], [peg2, line2], [peg3, line3], [peg4, line4]];

Begin Routine (PY):

for i in clickable:
    if mouse.isPressedIn(i[0]):
        i[1].ori += 90

Begin Routine (JS):

for (var i, _pj_c = 0, _pj_a = clickable, _pj_b = _pj_a.length; (_pj_c < _pj_b); _pj_c += 1) {
    i = _pj_a[_pj_c];
    if (mouse.getPressed(i[0])) {
        i[1].ori += 90;
    }
}

I am looking forward to any helpful advice. Thank you very much in advance.

I made the project public (I just started to set up the task, so there is not so much there):

Link: https://gitlab.pavlovia.org/lykanon/peg-v1-boring

I hope that anybody can help me, I am really stuck on this one and cant find a solution.

Hi @Lykanon, the mouse isPressedIn function does not currently work online, but try the following alternative in JS:

for (let i = 0; i < clickable.length; i++) {
  if (clickable[i][0].contains(mouse) && mouse.getPressed()[0] === 1) {
    clickable[i][1].ori += 90;
  }
}

Thank you for your help @dvbridges. I already used the getPressed method in my code posted above - with no success.

I tried the code and encountered the following error:

ReferenceError: i is not defined

:frowning:

Apologies @Lykanon, I have amended the code

Sorry to disturb you again @dvbridges, but as I tried the amended code, a new error popped up:

TypeError: Cannot read property ‘map’ of undefined

@Lykanon, Would you mind sharing the working URL of the task generating the error?

@dvbridges not sure, if this is what you meant, but here is the url generated by the pilot button in Pavlovia:

https://run.pavlovia.org/lykanon/peg-v1-boring/html/?__pilotToken=9bf31c7ff062936a96d3c8bd1f8f2ff3&__oauthToken=0e3183aea1566bb1f2abaabe6f37f7be7feb21e5b58c39b67a77cd5afe064f13

I shared the project link above. Thank you very much for looking into it! :slight_smile:

Thanks, thats it. I think the code needs to go into the “Each Frame” tab. At the moment, it is trying to check whether a mouse click has occurred in an object that has not been drawn yet. I was not able to test this, as the token expired.

Good morning @dvbridges,

when I inserted the code in “each frame” tab it produced a kind of odd outcome online. However, when I tried “end routine” it finally worked as intended. Thank you very much for your time and help!

Best regards