Jump to content

  • Log In with Google      Sign In   
  • Create Account

lietu

Member Since 18 May 2013
Offline Last Active May 18 2013 04:23 PM

Posts I've Made

In Topic: Newbie Javascript question

18 May 2013 - 03:57 PM

In JavaScript functions don't really have a "context" associated statically with them, it is evaluated dynamically .. setTimeout and setInterval basically "reset" the context and your call to "this.Tick" doesn't know what "this" is.

 

Change 

        this.tickIntervalId = setInterval(this.Tick, 1000/60); //Update at 60 fps
 
To
        this.tickIntervalId = setInterval(function() { this.Tick() }, 1000/60); //Update at 60 fps

To not lose the context
 
Edit: missing parenthesis on "To" -line's call to Tick()
 
Another edit:
Actually, not 100% sure of the "this.Tick()" inside that function either, the only sure-fire way to not lose "this" is to define something like var me = this; on the line above and call me.Tick() instead.
 
I.e.:
 
var me = this;
this.tickIntervalId = setInterval(function() { me.Tick() }, 1000/ 60);

PARTNERS