为什么该对象的位置为NaN?

时间:2019-09-29 13:06:35

标签: javascript paperjs

现在,我有一个网络套接字服务器,该服务器使用对象({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?

1 个答案:

答案 0 :(得分:1)

(我将其作为答案而不是评论,因为我在那里没有足够的空间)

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

这是根据您的代码示例改编的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
    }
});