Ryan1070

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

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. 

Share this post


Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...

  • Recently Browsing   0 members

    No registered users viewing this page.