psychopy.org | Reference | Downloads | Github

Mouse click issue when switching from pix to norm

Win10:
2020:
Standalone:
What are you trying to achieve?:
I have a fully working experiment and wanted to see if I could make it work using “norm” instead of “pixels” for online purposes.

All was fine, except for one part of my code snippet. Originally it was just instructing python to update a dictionary if a click was within 150 pixels:

if not skipThisTrial:
    if((mouse.x[0] >= xloc-150) and (mouse.x[0] <= xloc+150) and (mouse.y[0] >= yloc-150) and (mouse.y[0] <= yloc+150)):
        obs_dict[image] +=1
    else:
        obs_dict[image]=0

so I thought (hoped) it would be as easy as having the experiment set to norm in the new version of the experiment and popping in my chosen norm units (0.5):

if not skipThisTrial:
    if((mouse.x[0] >= xloc-0.5) and (mouse.x[0] <= xloc+0.5) and (mouse.y[0] >= yloc-0.5) and (mouse.y[0] <= yloc+0.5)):
        obs_dict[image] +=1
    else:
        obs_dict[image]=0

But of course life doesn’t work out as you plan it to, and the following error popped up:

AttributeError: ‘Mouse’ object has no attribute ‘x’

Anyone know why?

Cheers,
Tom

I’d still be really interested to understand why it didn’t work by just changing the units.

However, I did find an easy work around, by creating a polygon that represented my “correct zone” , making the polygon a clickable object in the mouse dialogue box, and adjusting the code above to the following:

if not skipThisTrial:
    if mouse.isPressedIn(polygon):
        obs_dict[image] +=1
    else:
        obs_dict[image]=0
       

and the js:

if ((skipThisTrial == false)) {
    if (polygon.contains(mouse)) {
        obs_dict[image] += 1;
    } else {
        obs_dict[image] = 0;
    }
}