Javascript 函数调用 Socket.io 和 Phaser

Javascript Functioncall Socket.io and Phaser

我在使用 Phaser 调用函数时遇到问题。我是一个 Javascript 菜鸟,不知道我做错了什么。

这是我代码的关键部分,所有代码都在同一个文件中。

BasicGame.Multiplayer = function (game) {
};

  BasicGame.Multiplayer.prototype = {

        create: function(){
            this.socket = io.connect('localhost:3010');
            this.socket.on('startGame', function () {
                console.log('ShouldStartGame');
                this.createActualGame();
            });
},

createActualGame: function(){
        // Define constants
       }
}

我的问题是未调用函数 this.createActualGame 并出现错误:"TypeError: this.createActualGame is not a function"。 但是控制台日志工作正常。

没有 socket.on(...) 代码工作正常,并且使用语句 "this.createActualGame()" 调用函数,但在 socket.on(...) 中它没有不工作,我不知道为什么,也不知道如何解决。非常感谢您的帮助。

谨致问候, 桑德曼爵士

试试看:

    create: function(){
        this.socket = io.connect('localhost:3010');
        this.socket.on('startGame', function () {
            console.log('ShouldStartGame');
            this.createActualGame();
        }.bind(this));
    },

或者只是:

BasicGame.Multiplayer.createActualGame();

socket = io.connect('localhost:3010');
        socket.on('startGame', function () {
            console.log('ShouldStartGame');
            this.createActualGame();
        }.bind(this));

是问题的解决方案。 (你把括号放错了)