PlayerTracker.js:Node.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;
在開始時,我得到:
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 }
但後來經過我打電話MoveTick,我得到:
{ _idleTimeout: 500,
_idlePrev: null,
_idleNext: null,
_idleStart: 4060813,
_onTimeout: [Function: wrapper],
_repeat: true }
它看起來像這個varibale重置。 我只是打開它var playerTracker = new PlayerTracker(this,5)
並與setInterval(playerTracker.moveTick,500)
打電話給我只是想保持的位置等。 你知道有什麼原因嗎?
你需要使用括號在你的第一個選項......'setInterval(function(){playerTracker.moveTick()},500);' – user1936097
@ user1936097:感謝您發現! – slebetman