| Reference | Downloads | Github

Advise on using class to create stimuli objects

Hi there,
Can someone have a look and see if I have overlooked something here, I have the script running in Python correctly (and lots more exiting code as well).
I have reduced the code from a larger project to highlight my issue.

I have a class to define a small box and created 3 instances of it in Begin Experiment

class myBox {
    constructor(pos, name, color) {
        this.pos = pos; = name;
        this.color = color;
        this.polygon = new visual.Rect(
        {"win": psychoJS.window, "name": ( + "_polygon"),
        "width": [0.05, 0.05][0], "height": [0.05, 0.05][1],
        "ori": 0, "pos": [this.pos[0], (this.pos[1] + 0.025)],
        "lineWidth": 1, "lineColor": [1, 1, 1],
        "lineColorSpace": "rgb", "fillColor": this.color,
        "fillColorSpace": "rgb", "opacity": 1, "depth": (- 2.0),
        "interpolate": true, "autoLog": false});
    Updates() {
        if ((this.polygon.status === NOT_STARTED)) {
yellowBox = new myBox([(- 0.2), (- 0.2)], "yellowBox", [1, 1, (- 1)]);
greenBox = new myBox([0, (- 0.2)], "greenBox", [(- 1), 1, (- 1)]);
blueBox = new myBox([0.2, (- 0.2)], "blueBox", [(- 1), (- 1), 1]);

In Each Frame I run the Updates() function


When run online in pilot mode the boxes are drawn on the screen in the correct places as defined by the constructor but they are all black and even if I write directly into the parameter, instead of this.color, it does not change the colour.

Can you direct me to some examples of using classes with Psychopy Components, I have been having issues with ImageStim as well when using a class to construct multiple instances and I think my problems might be related and I am assuming too much without the knowledge.


Have a look at my crib sheet for how I deal with colours by setting them as variables.


yellow = new util.Color([1, 1, 0]);

as a variable, that did the trick :slightly_smiling_face:

1 Like