Jump to content

Which socket connections clicked on the canvas with Socket.io


mla
 Share

Recommended Posts

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!

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
 Share

  • Recently Browsing   0 members

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