所以我正在建立一个三个js的游戏,并尝试通过socket.io进行多人游戏,所以我将所有角色加载到一个名为“我的服务器端的玩家”的阵列中
然后我将它传递给每个客户端,如此连接
Select A.subtaskID,
A.subtaskQueueID,
A.subtaskName,
B.timeComplete,
B.jobID,
B.approver
From Job_subtasks A
Left Join Job_subtasks_status B On A.subtaskID = B.subtaskID
And B.subtaskID = 2
And B.jobID = 840
在我的客户端上,syncPlayers看起来像这样
socket.on('addPlayer', function(username) {
players.push(username)
console.log(username + " joined")
console.log("online Users " + players)
socket.broadcast.emit('syncPlayers', players)
socket.emit('syncPlayers', players)
})
然后它调用这个发送服务器数据
socket.on('syncPlayers', function(players) {
players.forEach(function(value) {
if (value == username) {
console.log("not adding " + value + " thats you ")
loadPlayerdata(username)
} else {
console.log("player Online " + value);
newplayer = value;
loadPlayerdata(newplayer)
addPlayer(newplayer)
}
});
})
然后调用它并检索玩家名称和玩家位置的数据这是我的问题所在
function loadPlayerdata(playerName) {
console.log(playerName)
console.log("phase1")
socket.emit('loadPlayerdata', playerName)
}
我一直在socket.on('loadPlayerdata', function(data, username) {
toMove = threeObjects[username + "Char"]
if (data == "null" || "") {
console.log(username + " is new")
} else {
console.log(username + " Exists")
console.log(toMove)
toMove.position.set(world.spawnPointx, world.spawnPointy, world.spawnPointz)
}
即使我可以使用这个
Uncaught TypeError: Cannot read property 'position' of undefined
顺便说一句,我有一个对象
function addPlayer(playerName) {
var charObjectName = playerName + "Char"
var threeObject = models.tent1.mesh.clone();
scene.add(threeObject)
//threeObject.position.set(world.spawnPointx, world.spawnPointy, world.spawnPointz)
// set reference
threeObjects[charObjectName] = threeObject;
}
有人可以解释为什么它不会工作以及如何解决它
答案 0 :(得分:0)
您可以阅读this答案,了解点和括号表示法之间的区别。
您收到错误,因为tomove
似乎未定义,如果有任何新用户加入且对象为空,则点符号将抛出错误。
检查这是否有帮助。这会将对象键分配为用户名和位置作为一个值,这将是这样的数组,
{"usernamechar": {"position": [x,y,z]}}
socket.on('loadPlayerdata', function(data, username) {
if (data == "null" || "") {
console.log(username + " is new")
} else {
console.log(username + " Exists")
threeObjects[username + "Char"]["position"] = [world.spawnPointx, world.spawnPointy, world.spawnPointz]
}
}