Search code examples
javascriptpaperjs

Why is this object's position NaN?


Right now, I have a websocket server that sends the client the position of other players using an object ({pos:{x:32,y:46}}). I then put that into a point by doing var newposition = new Point(msg.pos.x,msg.pos.y) but then I never see the player appear.

I tried to console.log it, but it said the position was NaN. Then I tried to console.log the point, and it worked. I even tried not setting the position at all but it randomly sets its position to NaN for no reason that I can see.

This is my code for making a player join:

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)
}

And here's what happens when a player moves (without actually setting the position of the player.)

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)
}

And finally when the player joins:

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

On my backend, I just have it broadcast that someone joined with their id (who) and face (face).

There are no errors in the console and I am rather confused of why this happens... Why does it set itself to NaN?


Solution

  • (I put this as an answer rather than as a comment because I won't have enough space there)

    In order to find where the problem comes from, try simulating messages coming from the backend, to check that your client-side logic is right.

    Here is a sketch adapted from your code sample that can serve as a starting point for this task.
    I modified some things that didn't make sense to me but I think that you should be able to adapt it to your specific case.

    // 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
        }
    });