Quantcast
Channel: 2D Latest Topics
Viewing all articles
Browse latest Browse all 623

Which socket connections clicked on the canvas with Socket.io

$
0
0

Hi,

I'm wondering how to avoid simultaneous mousedown events. 

I have a canvas drawn on screen. When a player clicks events take place based on where the player clicked on the screen. 

If two players click the canvas at the exact same time, they should both be alerted with a message that no simultaneous moves are allowed.

I used an array to capture the move, however it seems to only register the move after the check and not before. 

Is there a simple way to capture which clients clicked at the same time?

//Listen for player click event
chainlinks.onmousedown = function(event) {
    var totCT;

	//update current player turn on client
	for (var i = 0; i < remotePlayers.length; i++) {
		if (remotePlayers[i].id == socket.id) {
			remotePlayers[i].currentTurn = true;
		}
	}
	//update current player turn on server
	for (var i in cPlayers) {
		if (cPlayers[i].id == socket.id){
			cPlayers[i].currentTurn = true;
		}
	}
	socket.emit('update-currentTurn', {remotePlayers: remotePlayers, cPlayers:cPlayers});
	
	if (totCT > 1) {
       //Simultaneous move happened - show invalid message 
	} else {
		//All good to go ahead with the click event
	}
}

Thanks!


Viewing all articles
Browse latest Browse all 623


<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>