0

PlayerTracker.js:

var util = require('util');
var PlayerTracker = function(GameServer, pos) {
  this.gs = GameServer;
  this.pos = pos;
  this.left = false;
  this.right = false;
  console.log("constructor" + util.inspect(this));
};

PlayerTracker.prototype.getPos = function() {
  return this.pos;
};

PlayerTracker.prototype.setPos = function() {
  return this.pos;
};

PlayerTracker.prototype.setLeft = function(left) {
  this.left = left;
};
PlayerTracker.prototype.setRight = function(left) {
  this.right = left;
};

PlayerTracker.prototype.moveTick = function() {
  console.log(util.inspect(this));
  if (this.left) {
    if (this.pos <= 0) {

    } else {
      this.pos--;
      this.left = false;
    }
  } else if (this.right) {
    if (this.pos >= 10) {

    } else {
      this.pos++;
      this.right = false;
    }
  }
};
module.exports = PlayerTracker;

At the Start i get:

constructor{ gs:       
{ config: { serverPort: 1010, testValue: 0 },
 socketServer:
  { domain: null,
    _events: [Object],
    _maxListeners: undefined,
    _server: [Object],
    _closeServer: [Function],
    options: [Object],
    path: null,
    clients: [Object] } },
  pos: 5,
  left: false,
  right: false }

but then after i call MoveTick, i get:

{ _idleTimeout: 500,
_idlePrev: null,
_idleNext: null,
_idleStart: 4060813,
_onTimeout: [Function: wrapper],
_repeat: true }

It looks like the this varibale resets. I simply open it with var playerTracker = new PlayerTracker(this,5) and call it with setInterval(playerTracker.moveTick,500) I just want to keep the position and so on. Do you know any reasons?

thefourtheye
  • 233,700
  • 52
  • 457
  • 497

2 Answers2

1

You either need to do:

setInterval(function(){playerTracker.moveTick()},500);

or:

setInterval(playerTracker.moveTick.bind(playTracker),500);

That's because the value of this in javascript depends on how you call the method.

When you do:

setInterval(playerTracker.moveTick,500);

What you're really doing is:

var x = playerTracker.moveTick;
setInterval(x,500);

Thus the setInterval is calling your function without the playTracker object. In which case the moveTick function is called as a regular function. And depending on weather or not you're in strict mode this is either undefined or the global object (window in browsers).

See this related answer for a complete (and up-to-date) description of how this works: How does the "this" keyword in Javascript act within an object literal?

Community
  • 1
  • 1
slebetman
  • 109,858
  • 19
  • 140
  • 171
0

When you call setInterval(playerTacker.moveTick, 500) what is happening is that the function moveTick is being called in a different context after 500ms, and the value of this will be an object pointing to the probably the interval timeout controller.

To make it run as you intend, you need to make sure the function is run in the context of the correct instance of the class PlayerTracker

try this: setInterval(function(){playerTracker.moveTick()}, 500)

yegodz
  • 5,031
  • 2
  • 17
  • 18