我有以下课程,我想遍历this.player,但无法使其正常工作。
我从最小的例子开始
在server.js中按以下方式调用游戏
const Game = require("./Game");
let game = new Game(io);
game.launch();
Game.js
class Game {
constructor(io) {
this.io = io; //socket.io
this.users = [];
}
waitForPlayers() {
let interval = setInterval(() => {
let size = Object.keys(this.users).length;
if (size >= 1) {
clearInterval(interval);
this.gameloop();
}
}, 2000)
}
gameloop() {
setInterval(() => {
for (let i in this.users) {
console.log(this.users[i]);
}
})
}
launch() {
this.io.on("connection", socket => {
socket.on("createUser", (arr) => {
this.createUser(socket, arr);
});
});
this.waitForPlayers();
}
createUser(socket, arr) {
this.users[socket.id] = socket;
this.users[socket.id].username = arr[0];
}
}
我在做什么错或我不明白什么?
Old Version