I have node.js emit serverGameData
that is converted to clientGameData
on the client side. In the initial stages I run a for loop to check for status changes in the positions listed in the menu. I don't think the why is really that important in this case but anyone has questions I will answer them.
the gamedata packet sent from node.js
serverGameData = {
positions: {
Captain: {
taken: false,
by: null
},
Helm: {
taken: false,
by: null
},
Weapons: {
taken: false,
by: null
},
Science: {
taken: false,
by: null
},
Engineering: {
taken: false,
by: null
},
Communications: {
taken: false,
by: null
}
}
The for loop ran in update() {}
for (var key in clientGameData.positions) {
var _pos = clientGameData.positions;
// Replaces button with grayed out sprite if position is taken by another player
if (_pos[key].taken && _pos[key].by != socket.id) {
if (this.availablePositions[key]) {
var _x = this['button' + key].x;
var _y = this['button' + key].y;
this['button' + key].kill();
this['button' + key] = this.add.sprite(_x, _y, 'mainMenuAtlas', key.toLowerCase() + '_t');
this.availablePositions[key] = false;
}
// Converts sprite back to fully functional button
} else if (!_pos[key].taken) {
if (!this.availablePositions[key]) {
var _x = this['button' + key].x;
var _y = this['button' + key].y;
this['button' + key].kill();
console.log("pre key: " + key);
/************************** PROBLEM AREA START ***********************************/
this['button' + key] = this.add.button(_x, _y,
'mainMenuAtlas', function () { this.buttonSelect(key); }, this, key.toLowerCase(), key.toLowerCase(), key.toLowerCase());
this.availablePositions[key] = true;
console.log("post key: " + key);
/*************************** PROBLEM AREA END ************************************/
}
}
}
I don't have problems with any of this code except the "problem area". When I console.log()
the key...both times it puts out the proper value. It even puts out the proper value in the key.toLowerCase()
however, the function I set up: this.buttonSelect(key)
will take the value of the last key iterated in the for loop. In this case, Communications
. If I switch the order on the server, though, it will reflect here. Whatever is last in the serverGameData
and thus clientGameData
will be put into that function.
If this is just something wonky with phaser I can accept that but I can't come up with a way to fix it.