Hi, everyone.
Ok, I am developing a real-time shooter.
I use NodeJS and socketio on the server.
In the game, other players can kill user's player.
I implemented this on server-side like this:
function attackPlayer(attacked_id, performer_id) {
var attackedPlayer = survivors[attacked_id];
var performer_weapon_damage = items.weapons[survivors[performer_id].weapon].damage;
var attacked_armor = items.headdress[survivors[attacked_id].hat].protection;
survivors[attacked_id].health -= performer_weapon_damage - attacked_armor;
if (survivors[attacked_id].health <= 0) {
//Works perfectly
var ind = sectors[survivors[attacked_id].sectorid].players.indexOf(attacked_id);
sectors[survivors[attacked_id].sectorid].players.splice(ind, 1);
survivors.splice(attacked_id, 1);
io.emit('onkill', {ID: attacked_id});
return true;
}
return false;
}
I've been checked this code and it seems to be work good.
But when I receive it(onkill
event) on client-side something goes wrong, becouse players array doesn't want to update.
Here my client-side code:
socket.on('onkill', function(data) {
console.log(players);
players = players.filter(function(p, i) {
return i !== data.ID;
});
console.log(players);
});
players
array doesn't change : (
Also I tried to write the following to remove the killed player from the array:
1. players.splice(data.ID,1);
2. delete players[data.ID]; //I know, that it is unsafe
3. players[data.ID] = -1; //Also tried to set to undefined and {}
Also I tried to send the updated players array from the server to client.
But with all tries result was the same...
Maybe you have some ideas to make it work? Please, any help is important.
via Artem Griukov
No comments:
Post a Comment