Skip to content

Instantly share code, notes, and snippets.

@itsjoekent
Last active August 29, 2015 14:28
Show Gist options
  • Save itsjoekent/e54e481d8e362a49c5cf to your computer and use it in GitHub Desktop.
Save itsjoekent/e54e481d8e362a49c5cf to your computer and use it in GitHub Desktop.
socket.on('update-pos', function(data) {
if(allMonsters[data.id] == undefined) {
allMonsters[data.id] = new Monster(data.x, data.y, data.type, data.id, data.direction);
}
else {
// Relevant part, by getting to this point we assume the human
// does in fact exist, as it's in the list of human ID's
posUpdates.push(data);
}
});
Game.update = function() {
...
posUpdates.forEach(function(element, index, array) {
var theMonster = allMonsters[element.id];
// Due to the previous test, we assumed that the monster here exists,
// however it disconnected before this function had an opritunuty to run and it's now undefined
// this causes and error and the client to fail
theMonster.x = element.x;
theMonster.y = element.y;
theMonster.animations.currentAnimation = 'move';
theMonster.direction = element.direction;
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment