Cannot read property 'keyboard' of undefined

I’m getting this error

cannot read property ‘keyboard’ of undefined

const onTriggerData = function (pos, name)
        {

            console.log(pos, name);
            if(this.app.keyboard.wasPressed(pc.KEY_Y)){
                this.triggerPos = pos;
                console.log('key y pos', this.triggerPos);
            }
        };

this.app.on('triggerData', onTriggerData);

I assume this.app not has scope?

Yes, exactly, to give scope to an anonymous function you need to use bind or a local var:

const onTriggerData = function (pos, name)
        {

            console.log(pos, name);
            if(this.app.keyboard.wasPressed(pc.KEY_Y)){
                this.triggerPos = pos;
                console.log('key y pos', this.triggerPos);
            }
        }.bind(this);
2 Likes