If mouse is pressed change color

Hi!
I want to change colors of polygon shapes when they are clicked.
My experiment works “offline” but not online.

Here is my code in the each frame component (Auto->JS):

Python:

for h in houses:
    if mouse.isPressedIn(h) and h.fillColor == "red":
        clicked = True
        h.fillColor = "white"
        core.wait(0.15)
    elif mouse.isPressedIn(h) and h.fillColor == "white":
        clicked = True
        h.fillColor = "red"
        core.wait(0.15)

JS:

for (var h, _pj_c = 0, _pj_a = houses, _pj_b = _pj_a.length; (_pj_c < _pj_b); _pj_c += 1) {
    h = _pj_a[_pj_c];
    if ((mouse.isPressedIn(h) && (h.fillColor === "red"))) {
        clicked = true;
        h.fillColor = "white";
        core.wait(0.15);
    } else {
        if ((mouse.isPressedIn(h) && (h.fillColor === "white"))) {
            clicked = true;
            h.fillColor = "red";
            core.wait(0.15);
        }
    }
}

Maybe it has something to do with the “and” operator? In another experiment without the “and” it works online. I also tried it with setFillColor but this also did not work.

Thank you very much for any help!

1 Like

Hi!
Unfortunately, I could not solve my problem above yet.
I found a great documentation of wakecarter: PsychoPy Python to Javascript crib sheet - Google Docs

I tried to follow the advice to use RGB instead of names. Accordingly, in begin experiment of my code component I defined red and white :

red = [1,0,0]
white = [1,1,1]

and adapted the above code in my code component (each frame):

    if mouse.isPressedIn(h) and h.fillColor == red:
        clicked = True
        h.fillColor = white
        core.wait(0.15)

However, now I get the error message:
if mouse.isPressedIn(h) and h.fillColor == red:
ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()

What I am doing wrong? Can anybody help me with this problem?

Solved with this post:

Many thanks to Becca! Great!