2015-06-05 93 views
0

個人hello有一個服務器,他給出了一個簡單的錯誤,但不知道如何解決,並且對象顯然是正常的,請參閱下面的代碼。node.js服務器給出的錯誤

var io = require('socket.io').listen(8000); 

    var players = {}, count = 0; 

io.sockets.on('connection', 
    function (socket) 
    { 
     var id = count++; //assign number 
     var player = players[id] = new Player(id, 10, 1); //create new  player 
    player.socket = socket; 


    socket.on('chat', 
     function (data) 
     { 
      var message = player.name + ': ' + data.message; 
      socket.emit('chat', { message: message}); 
      socket.broadcast.emit('chat', { message: message}); 
     }); 

    socket.on('position', 
     function (data) 
     { 
      player.x = data.playerX; 
      player.y = data.playerY; 
      player.z = data.z; 


      socket.emit('position', {pid : player.id, playerX : player.x, playerY : player.y, z : player.z}); 
      socket.broadcast.emit('position', {pid : player.id, playerX : player.x, playerY : player.y, z : player.z}); 
     }); 

    socket.on('register', 
     function (name) 
     { 
      player.name = name; 
      player.registered = true; 
      socket.emit('chat', { message : "Server: bem vindo ao server " + player.name + '!'}); 
      socket.broadcast.emit('chat', { message : "Server: bem vindo ao server " + player.name + '!'}); 

      for(var p in players) 
      { 
       //send initial update 
       if(!players[p].disconnected) 
       { 
        socket.emit('addPlayer', {pid: players[p].id, x: players[p].x, y: players[p].y, name: players[p].name, sessionid: socket.id}); 
        if(players[p].id != player.id) 
         players[p].socket.emit('addPlayer', {pid: player.id, x: player.x, y: player.y, name: player.name}) 
       } 
      } 

     }); 

    socket.on('disconnect', 
     function (socket) 
     { 
      player.disconnected = true; 
      delete players[player.id]; 
      player = null; 
     }); 

}); 



function Player(id, x, y, z) { 
    this.id = id; 
    this.x = x; 
    this.y = y; 
    this.z = z; 
    this.name = ""; 
    this.disconnected = false; 
    this.registered = false; 
    this.socket = null; 
} 

合金正常的服務器,但是,當它接收該消息的數據

this.z = z; 

「this.z = Z沒有定義」

+1

新玩家(id,10)你只傳入兩個參數,所以y和z將被定義爲 –

+1

另外,'Player'參數是大寫Z,'this.z'正在尋找小寫z。 – Andy

+0

這是對的謝謝你,我沒有看到z被大寫。現在它工作了。 –

回答

-1

它可能是由於你的玩家對象需要在其結尾處返回this

+0

我必須離開它: var player = players [id] = new Player(id,10,1); –

+0

是的,但在底部說'''功能播放器'''不返回任何東西來創建對象是什麼即時通訊試圖說 –

+0

謝謝了現在工作 –

相關問題