I
I
Iya272021-06-22 23:52:41
Socket.io
Iya27, 2021-06-22 23:52:41

How to make a move?

I want to move the user behind the green circle, but I can't.
Gives "Uncaught ReferenceError: io is not defined" in console . What am I doing wrong?

app2.js:

let players = {};
    io.on('connection', function(socket) {
      socket.on('new player', function() {
        players[socket.id] = {
          x: 300,
          y: 300
        };
      });
      socket.on('movement', function(data) {
        var player = players[socket.id] || {};
        if (data.left) {
          player.x -= 5;
        }
        if (data.up) {
          player.y -= 5;
        }
        if (data.right) {
          player.x += 5;
        }
        if (data.down) {
          player.y += 5;
        }
      });
    });
    setInterval(function() {
      io.sockets.emit('state', players);
    }, 1000 / 60);


<html>
      <head>
        <title></title>
        <style>
          canvas {
            width: 800px;
            height: 600px;
            border: 1px solid black;
          }
        </style>
      </head>
      <body>
        <canvas id="canvas"></canvas>
      </body>
      <script src="app2.js"></script>
      <script>
      var movement = {
      up: false,
      down: false,
      left: false,
      right: false
    }
    document.addEventListener('keydown', function(event) {
      switch (event.keyCode) {
        case 65: // A
          movement.left = true;
          break;
        case 87: // W
          movement.up = true;
          break;
        case 68: // D
          movement.right = true;
          break;
        case 83: // S
          movement.down = true;
          break;
      }
    });
    document.addEventListener('keyup', function(event) {
      switch (event.keyCode) {
        case 65: // A
          movement.left = false;
          break;
        case 87: // W
          movement.up = false;
          break;
        case 68: // D
          movement.right = false;
          break;
        case 83: // S
          movement.down = false;
          break;
      }
    });
    socket.emit('new player');
    setInterval(function() {
      socket.emit('movement', movement);
    }, 1000 / 60);  
      </script>
    </html>

Answer the question

In order to leave comments, you need to log in

1 answer(s)
V
Vladimir, 2021-06-23
@Iya27

const io = require('socket.io');

let players = {};
    io.on('connection', function(socket) {
      socket.on('new player', function() {
        players[socket.id] = {
          x: 300,
          y: 300
        };
      });
      socket.on('movement', function(data) {
        var player = players[socket.id] || {};
        if (data.left) {
          player.x -= 5;
        }
        if (data.up) {
          player.y -= 5;
        }
        if (data.right) {
          player.x += 5;
        }
        if (data.down) {
          player.y += 5;
        }
      });
    });
    setInterval(function() {
      io.sockets.emit('state', players);
    }, 1000 / 60);

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question