Jump to content

game.timer.event & scope


charlie_says
 Share

Recommended Posts

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

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

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...