• Advertisement


  • Content count

  • Joined

  • Last visited

Community Reputation

101 Neutral

About lietu

  • Rank
  1. Newbie Javascript question

    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);
  • Advertisement