I have a nodeJS app requiring player.js
. In player.js
I define Player
and add the method Player.updatePacket
, but when I require it in main.js
and create a player instance, player.updatePacket
is undefined.
player.js:
module.exports.PLAYER_LIST = {};
var Player = exports.Player = {}
//constructor
Player.create = function(id) {
var tmp = {
id: id,
x: 0,
y: 0
};
PLAYER_LIST[id] = tmp;
return tmp;
}
Player.updatePacket = function() {
return {
id: this.id,
x: this.x,
y: this.y
}
}
main.js:
var Player = require('./player.js')
//get called by socket.io when a client connects
//didn't include socket.io setup in example for brevity, but this function
//is called as expected.
io.sockets.on('connection', function(socket){
var player = new Player(socket.id)
});
setInterval(function() {
var dataArr = [];
for(var i in Player.PLAYER_LIST) {
var player = Player.PLAYER_LIST[i];
console.log(player); //this logs: [Function]
dataArr += player.updatePacket(); //throws TypeError: not a function
}
broadcast("update", dataArr);
}, 1000/25);
I have tried moving the export statement to the bottom of player.js
and putting updatePacket: function() {/*function contents*/}
in the tmp object, and I still get the same error. Any help or explanation is appreciated.