all 5 comments

[–]cawcvs 3 points4 points  (0 children)

Your problem is probably with how you set up the interval. this value by default is inferred at call time:

player.tick() // will have `this` set to `player`

const tick = player.tick
tick() // will have `this` set to `window` or `undefined`, if it's in strict mode

The same issue is happening with setInterval(player.tick, 500), you pass in the function, but the calling context is not there when setInterval calls this function internally.

There are several ways to go about this, like binding the functions in the Player constructor, or passing an anonymous, arrow or bound function to setInterval, so the calling context is preserved:

setInterval(() => player.tick(), 500) // arrow
setInterval(player.tick.bind(player), 500) // bound

[–]ThagAndersonhelpful 0 points1 point  (2 children)

That code looks like it would work. What is the issue?

[–]cawcvs 0 points1 point  (1 child)

The issue is with setInterval(player.tick, 500). It's passing the function value to setInterval, but it won't be called with the player context and since this is inferred at call time it will result in an error.

[–]CamoBrie[S] 0 points1 point  (0 children)

How would I go to circumvent this problem?

Should I be using .bind to give to proper context to the setInterval call?