R
R
Ruslan Website2021-03-14 23:39:02
Socket.io
Ruslan Website, 2021-03-14 23:39:02

How to pass element position to other connected clients after event.target.prepend()?

Just learning Node.js and Socket.io.

There is a function on the client.

function GameMap(event) {

    let target = event.target;

    switch(target.className) {

    case 'coast':

    Place('img/coast.jpg');
    break;

    case 'water':

    Place('img/water.png');
    break;

    case 'land':

    Place('img/land.jpg');
    break;
}

    if (flag == true && (target.className == 'land' || target.className == 'coast')) {

        target.prepend(player);

        socket.emit('move', 'НАХУЙ');

        

        flag = false;

        flag2 = false;

        

        

    } else if (flag3 == true && (target.className == 'land' || target.className == 'coast')) {

        playermenu.style.display = "none";

        flag2 = false;

        flag3 = false;
    }

    socket.on('move2', function(data) {

        coast2.innerHTML = data;

    });
}


And a simple reception from sending to the server.

socket.on('move', function(data) {

        io.emit('move2', data);
    });


If you remove the client-server part of the code, then the function simply moves the element (div) to the beginning of another element (target.prepend(player)), naturally, according to the event.target principle. But I add the code to send an event to the server and the event associated with it is already from the server to all other connected clients, it is logical that nothing will change for them, because other clients did not have clicks, and accordingly event.target will not work and because of this it will not work receiving an event
socket.on('move2', function(data) {coast2.innerHTML = data;});


So, how to make the fucking character move both for the one who made the move and for those who are just looking at the screen? as part of my task, namely stupidly moving div elements. The first thing that comes to mind is to put an id on all elements of the map, i.e. determine where event.target hits by id and already by this id display the movement of the character for other connected clients. But it seems that there should be a better solution. I know that it’s probably easier to do all this through canvas and in general a lot of things can be done differently, but for now I want to understand how to implement this possibility in my task, understand the essence, and then move on.

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question