Jump to content

How would I animate players using socket.io and phaser 3?


Ryan1070
 Share

Recommended Posts

I was wondering how I could animate my player and the other players once they start moving. I tried to create an event called "animate player" in my server file like this: 

    socket.on('playerMovement', function (movementData) {
        players[socket.id].x = movementData.x
        players[socket.id].y = movementData.y
        players[socket.id].playerFrame = movementData.playerFrame
        socket.broadcast.emit('playerMoved', players[socket.id]);
        socket.emit('animatePlayer', players[socket.id], this.anims)
    });

Then in my phaser scene file I have my animations defined in the create function:

        this.anims.create({
            key: "up",
            frames: this.anims.generateFrameNumbers("player", {start: 12, end: 15}),
            frameRate: this.frameRate,
            repeat: -1, // Infinite loop
        });

        this.anims.create({
            key: "left",
            frames: this.anims.generateFrameNumbers("player", {start: 4, end: 7}),
            frameRate: this.frameRate,
            repeat: -1, // Infinite loop
        });

        this.anims.create({
            key: "right",
            frames: this.anims.generateFrameNumbers("player", {start: 8, end: 11}),
            repeat: -1,
            frameRate: this.frameRate, // Infinite loop
        });

Finally in the update function in the same phaser scene file I handle the "animate player" event that is back in the server file:

        this.socket.on('animatePlayer', function (playerInfo) {
            this.playerObj.anims.play("left")
        });

The console returns "Uncaught TypeError: Cannot read property 'anims' of undefined". I have my playerObj defined in add new player, which is also a function that is called from the server, when a player joins:

    addNewPlayer(self, playerInfo) {
        this.numberOfPlayers++;
        this.playerObj = this.physics.add.sprite(playerInfo.x, playerInfo.y, "player")
    }

 

Any help at all would be greatly appreciated. 

Link to comment
Share on other sites

 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...