Class Varible Resseting

I am using Node JS but I have folders for Kaboom too.

init();
loadSprite("kaboom", "res/kaboom.png");

scene("main", () => {
  let socket = io();

  let objects = {};
  socket.on("state", e => {
    for (let i = 0; i < e.players.length; i++) {
      let playerData = e.players[i];
      playerData.x+=playerData.aX;
      playerData.y+=playerData.aY;
      playerData.aX*=0.99;
      playerData.aY*=0.99;
      for(var j = 0; j < e.players.length; j++) {
        if(i !== j) {
          let playerData2 = e.players[j];
          if(playerData.x > playerData2.x - 30 && playerData.y > playerData2.y - 30 && playerData.x < playerData2.x + 30 && playerData.y < playerData2.y + 30 && playerData.tagCool <= 0) {
            playerData.hp-=1;
            playerData2.hp-=1;
            playerData.aX = 3;
            playerData.aY = 3;
          }
        }
      }
      if (!objects[playerData.id]) {
        objects[playerData.id] = add([
          rect(30, 30),
          pos(playerData.x, playerData.y),
          color(playerData.color[0], playerData.color[1], playerData.color[2]),
        ]);
        objects[playerData.id + 999999] = add([
          rect(30, 5),
          pos(playerData.x, playerData.y + 35),
          scale(1),
          origin("center"),
          color(0, 200, 0)
        ]);
        objects[playerData.id + 9999999] = add([
          text(playerData.id),
          pos(playerData.x, playerData.y - 35),
          scale(1),
          origin("center"),
        ]);
      }
      let playerObject = objects[playerData.id];
      playerObject.pos.x = playerData.x;
      playerObject.pos.y = playerData.y;
      let playerObject2 = objects[playerData.id + 999999];
      playerObject2.pos.x = playerData.x;
      playerObject2.pos.y = playerData.y + 35;
      playerObject2.width = 3 * playerData.hp;
      let playerObject3 = objects[playerData.id + 9999999];
      playerObject3.pos.x = playerData.x;
      playerObject3.pos.y = playerData.y - 35;
    }
  });
	keyDown("left", () => {
    socket.emit("moveLeft");
  });

  keyDown("up", () => {
    socket.emit("moveUp");
  });

  keyDown("right", () => {
    socket.emit("moveRight");
  });
  keyDown("down", () => {
    socket.emit("moveDown");
  });

});

start("main");

The other code strip

const WORLD = {
  WIDTH: 1240,
  HEIGHT: 880
};

const COLORS = {
  RED: [1, 0, 0],
  GREEN: [0, 1, 0],
  BLUE: [0, 0, 1]
}

const COLORS_LIST = Object.values(COLORS);

function randomIntBelow(max) {
  return Math.floor(Math.random() * max);
}

function randomColor() {
  return [Math.random(), Math.random(), Math.random()];
}

class Player {
  constructor(id) {
    this.id = id;
    this.hp = 10;
    this.mHp = 10;
    this.tagCool = 0;
    this.x = randomIntBelow(WORLD.WIDTH - 30);
    this.y = randomIntBelow(WORLD.HEIGHT - 30);
    this.color = randomColor();
    this.speed = 6;
    this.aX = 0;
    this.aY = 0;
  }

  moveRight() {
    this.x += this.speed;
    if (this.x > WORLD.WIDTH) {
      this.x -= WORLD.WIDTH;
    }
  }

  moveLeft() {
    this.x -= this.speed;
    if (this.x < 0) {
      this.x += WORLD.WIDTH;
    }
  }

  moveUp() {
    this.y -= this.speed;
    if (this.y < 0) {
      this.y += WORLD.HEIGHT;
    }
  }

  moveDown() {
    this.y += this.speed;
    if (this.y > WORLD.HEIGHT) {
      this.y -= WORLD.HEIGHT;
    }
  }
}

class Game {
  constructor(io) {
    this.io = io;
    this.connected_players = {};
  }

  listen() {
    let index = 0;
    this.io.on("connection", socket => {
      let id = ++index;
      let player = new Player(id);
      this.connected_players[id] = player;
      socket.on("moveLeft", () => {
        player.moveLeft();
      })
      socket.on("moveRight", () => {
        player.moveRight();
      })
      socket.on("moveUp", () => {
        player.moveUp();
      })
      socket.on("moveDown", () => {
        player.moveDown();
      })
      socket.on("disconnect", () => {
        delete this.connected_players[id];
      })
    });

    setInterval(() => {
      this.io.emit("state", { 
        "players": Object.values(this.connected_players)
      });
    }, 15)
  }
}

module.exports = io => new Game(io);

The HP for the players keep resetting for some reason idk why.
Link to full code:
https://replit.com/@thomasIguess/ArenaMode?v=1

I recommend asking on the Kaboom Discord Server

ok, thanks. I will see the dicord server

1 Like