为什么这个对象的位置是 NaN?

Why is this object's position NaN?

现在,我有一个 websocket 服务器,它使用对象 ({pos:{x:32,y:46}}) 向客户端发送其他玩家的位置。然后我通过 var newposition = new Point(msg.pos.x,msg.pos.y) 把它变成一个点,但后来我再也没有看到玩家出现。

我试着 console.log 它,但它说位置是 NaN。然后我尝试 console.log 点,它起作用了。我什至试过根本不设置位置,但它随机将其位置设置为 NaN,我看不出任何原因。

这是我让玩家加入的代码:

function addPlayer(nickname,position,color,uid,size,face) {
    var circle = new Path.Circle(position,size)
    var face = new Raster("/faces/"+face+"/face.png")
    face.rescale(40,40)
    face.position = position
    var masker = new Group({
        children: [circle, face],
        clipped: true
    });
    face.onLoad = function() {
        // Fit the circle snugly around the image:
        circle.fitBounds(face.bounds);
    };

    circle.fillColor = color
    console.log(nickname + " has joined the server")
    console.log(players)
    players[uid] = {
        circle: circle,
        nickname: nickname,
        entirething: masker,
        face: face
    }
    console.log(circle.position)
}

下面是玩家移动时发生的情况 (没有实际设置玩家的位置。)

if(msg.event == "move" && msg.who != cuid) {
    var thepoint = new Point(msg.pos.x,msg.pos.y)
    console.log(thepoint)
    console.log(players[msg.who].circle.position)
}

最后当玩家加入时:

if(msg.event == "join" && msg.who != cuid) {
    addPlayer(msg.username,{x:0,y:0},"dodgerblue",msg.who,20,msg.face)
}

在我的后端,我只是让它广播有人加入了他们的 ID (who) 和面孔 (face)。

控制台中没有错误,我很困惑为什么会发生这种情况...为什么它会将自己设置为 NaN?

(我把它作为答案而不是评论,因为我没有足够的 space)

为了找出问题的根源,尝试模拟来自后端的消息,以检查您的客户端逻辑是否正确。

这是根据您的代码示例改编的 sketch,可以作为此任务的起点。
我修改了一些对我来说没有意义的东西,但我认为你应该能够根据你的具体情况进行调整。

// Init global variables.
var cuid = 999;
var players = {};

function addPlayer(nickname, position, color, uid, size, face) {
    // Init image and circle.
    var circle = new Path.Circle(position, size);
    var image = new Raster(face);
    image.onLoad = function() {
        // Make image fit circle bounds.
        image.fitBounds(circle.bounds);
    };
    // Use circle as image clip mask.
    var masker = new Group({
        children: [circle, image],
        clipped: true
    });

    console.log(nickname + ' has joined the server');

    // Store player.
    players[uid] = {
        circle: circle,
        nickname: nickname,
        entirething: masker,
        face: image
    };
}

// On message...
function onMessage(msg) {
    // If message concerns current player...
    if (msg.who === cuid) {
        // ...don't do nothing.
        return;
    }
    // If message is a move event...
    else if (msg.event == 'move' && msg.who != cuid) {
        // ...update player position.
        players[msg.who].entirething.position = new Point(msg.pos.x, msg.pos.y);
    // If message is a join event...
    } else if (msg.event == 'join' && msg.who != cuid) {
        // ...add a new player.
        addPlayer(msg.username, { x: 0, y: 0 }, 'dodgerblue', msg.who, 20, msg.face);
    }
}

//
// Simulate messages reception.
//

// Add player 1
onMessage({
    event: 'join',
    who: 1,
    username: 'player 1',
    face: 'http://assets.paperjs.org/images/marilyn.jpg'
});

// Move player 1
onMessage({
    event: 'move',
    who: 1,
    pos: {
        x: 50,
        y: 50
    }
});

// Add player 2
onMessage({
    event: 'join',
    who: 2,
    username: 'player 2',
    face: 'http://assets.paperjs.org/images/marilyn.jpg'
});

// Move player 2
onMessage({
    event: 'move',
    who: 2,
    pos: {
        x: 500,
        y: 125
    }
});