Jump to content

Bizzare Display Bug - Multiplayer


Recommended Posts

Does this behavior sound familiar to anyone?

Working on a multiplayer client.  I have a player prototype that is created when the client is told that a new player has joined the server:

Player = function (id, game, player) {
	this.x = 300;
	this.y = 300;
	this.id = id;
	this.dir = "-1";
    this.game = game;
    this.player = player;
    this.sprite = game.add.sprite(300, 300, 'ms',1);
	this.sprite.animations.add('right', [6,7,8],4,true);
	this.sprite.animations.add('left', [3,4,5],4,true);
	this.sprite.animations.add('down', [0,1,2],4,true);
	this.sprite.animations.add('up', [9,10,11],4,true);

	game.physics.enable(this.sprite, Phaser.Physics.ARCADE);	
	this.sprite.body.setSize(72, 32,28,96);

	this.nametext = game.add.text(236, 364, this.player, { font: '14px Arial', fill: '#ffffff' });

That player creation is stored in an array...  Then whenever I receive an update i pool through the array and update accordingly:

// Update User Info
socket.on('userupdate',function(data) {
	info = data.split(',');
		info = data.split(',');
		for(a=0; a<totalplayers; a++){			
			if(playerArray[a].player == info[0]){
				playerArray[a].sprite.x = info[1];
				playerArray[a].sprite.y = info[2];
				playerArray[a].dir = info[3];

I've checked that the data comes in is proper.  Here is the REALLY weird thing.

When im clicked on the screen the sprite does not show up at all.  When i click OFF the browser and lose focus, the sprite appears and updates correctly...   I`m sure I am making a dumb mistake??



Link to comment
Share on other sites

My guess is that "if(playerArray[a].player == info[0])" will never be true, as it's a reference comparison and both objects are not pointing to the same reference (one is a local object, other comes from the network). 

You should not send the whole object over the network, only what's needed (like x, y, dir). Websockets are fragile, don't hammer them with useless data. And for comparison, assign a unique id to your object, and do something like "if(playerArray[a].player.id == info[0].id)"

Link to comment
Share on other sites


  • Recently Browsing   0 members

    • No registered users viewing this page.
  • Create New...