charlie_says Posted May 13, 2014 Share Posted May 13, 2014 I have a query about using game.time.events When I use this, I think I'm losing the scope of where the event is set as this code: game.time.events.add( 100, this.doStepBoost );gives this error: this.doStepBoost is not a function (removing the this doesn't remedy the issue). I'm sure this is because I'm using the event incorrectly, any pointers as to where I'm going wrong? Link to comment Share on other sites More sharing options...
jpdev Posted May 13, 2014 Share Posted May 13, 2014 Try:game.time.events.add( 100, this.doStepBoost.bind(this) );to preserve the scope. Link to comment Share on other sites More sharing options...
stasuss Posted May 13, 2014 Share Posted May 13, 2014 Why not use the callback context, which is the third parameter?game.time.events.add(100, this.doStepBoost, this);In this case 'this' in the callback will be set to the third parameter, which is 'this'.You can also use closures:var that = this;game.time.events.add(100, this.doStepBoost);and inside callback:that.doStepBoost Link to comment Share on other sites More sharing options...
charlie_says Posted May 13, 2014 Author Share Posted May 13, 2014 The first example works fine! Link to comment Share on other sites More sharing options...
Recommended Posts