i’m trying to mutiplayer working for my game, and i follwed the tutorial. pls help, here’s my code
var Network = pc.createScript('network');
// static variables
Network.id = null;
Network.socket = null;
// initialize code called once per entity
Network.prototype.initialize = function() {
this.player = this.app.root.findByName('Player');
this.other = this.app.root.findByName('Other');
//https://glitch.com/edit/#!/valuable-spice-mouth
var socket = io.connect('https://valuable-spice-mouth.glitch.me'); // Glitch hosted server
Network.socket = socket;
socket.emit ('initialize');
var self = this;
socket.on ('playerData', function (data) {
self.initializePlayers (data);
});
socket.on ('playerJoined', function (data) {
self.addPlayer(data);
});
socket.on ('playerMoved', function (data) {
self.movePlayer(data);
});
socket.on ('killPlayer', function (data) {
self.removePlayer(data);
});
};
Network.prototype.initializePlayers = function (data) {
this.players = data.players;
Network.id = data.id;
for(var id in this.players){
if(id != Network.id){
this.players[id].entity = this.createPlayerEntity(this.players[id]);
}
}
this.initialized = true;
console.log('initialized');
};
Network.prototype.addPlayer = function (data) {
this.players[data.id] = data;
this.players[data.id].entity = this.createPlayerEntity(data);
};
Network.prototype.movePlayer = function (data) {
if (this.initialized && !this.players[data.id].deleted) {
this.players[data.id].entity.rigidbody.teleport(data.x, data.y, data.z);
}
};
Network.prototype.removePlayer = function (data) {
if (this.players[data].entity) {
this.players[data].entity.destroy ();
this.players[data].deleted = true;
}
};
Network.prototype.createPlayerEntity = function (data) {
var newPlayer = this.other.clone();
newPlayer.enabled = true;
this.other.getParent().addChild(newPlayer);
if (data)
newPlayer.rigidbody.teleport(data.x, data.y, data.z);
return newPlayer;
};
// update code called every frame
Network.prototype.update = function(dt) {
this.updatePosition();
};
Network.prototype.updatePosition = function () {
if (this.initialized) {
var pos = this.player.getPosition();
Network.socket.emit('positionUpdate', {id: Network.id, x: pos.x, y: pos.y, z: pos.z});
}
};
can you tell me if anything is worng