[SOLVED] Error after firing an event

Hello Everybody,

I’ve a new error, I can’t get fixed :frowning: in my game CubeWorld

I fire an event after a button was pressed: {left or right arrow}

var Button = pc.createScript(‘button’);

Button.prototype.initialize = function() {
this.entity.element.on(‘click’, this.onClick, this);
};
Button.prototype.update = function(dt) { };

Button.prototype.onClick = function (){
this.app.fire(this.entity.name, true);
};

this works fine, except a situation when the button is destroyed after the event has been catched… Now I get teh following error:

Error loading scripts. Open the browser console for details.

and in console:

Uncaught TypeError: Cannot read property ‘active’ of null
at ButtonComponent._fireIfActive (playcanvas-stable.dbg.js:46242)
at ButtonComponent._onClick (playcanvas-stable.dbg.js:46238)
at ElementComponent.fire (playcanvas-stable.dbg.js:741)
at ElementInput._fireEvent (playcanvas-stable.dbg.js:71542)
at ElementInput._onElementMouseEvent (playcanvas-stable.dbg.js:71428)
at ElementInput._handleUp (playcanvas-stable.dbg.js:71229)

what should i do?
It’s the first time ever, I used a ButtonElement…

thanx :cucumber:

Hi @Gurki,

I didn’t get too much time to look at it, but my guess would be that it’s related to the way the button’s entity is being destroyed because the ElementComponent event to destroy it is being carried out before a different ButtonComponent event is called. Don’t take my word for it though. The explanation is just a shot in the dark.

That being said, in my fork, changing your button script to:

Button.prototype.initialize = function() {
this.entity.button.on(‘click’, this.onClick, this);
};

instead of:

Button.prototype.initialize = function() {
this.entity.element.on(‘click’, this.onClick, this);
};

seemed to clear the error for me.

3 Likes

works perfect, thanks a lot :hugs:

1 Like