Phaser.Signal: listener 是 add() 的必需参数,应该是一个函数

Phaser.Signal: listener is a required param of add() and should be a Function

我目前正在使用 Phaser 2.x.The 问题出在 create 方法中。我将函数作为参数传递给 this.claimButton.events.onInputDown.add() 方法。但是它无法引用我目前引用的函数 to.What 我可以为此做些什么吗?

我试过使用 this.game.claimTicket()this.game.claimTicket 并且还尝试将 this.claimTicket 作为参数放入 this.claimButton.events.onInputDown.add() 中。但我仍然收到错误提示 Uncaught Error: Phaser.Signal: listener is a required param of add() and should be a Function. 但是当我尝试 this.claimButton.events.onInputDown.add(function () { console.log('hello') }) 它工作得很好。

var GameState = {
    //initiate game settings
    init: () => {
        //adapt to screen size, fit all the game
        this.game.scale.scaleMode = Phaser.ScaleManager.SHOW_ALL;
        this.game.scale.pageAlignHorizontally = true;
        this.game.scale.pageAlignVertically = true;
    },
    preload: () => {
        this.game.load.image('background', 'assets/images/background.jpg')
        this.game.load.image('ticket', 'assets/images/ticket2.jpg')
        this.game.load.image('claimButton', 'assets/images/claimButton.png')
    },
    create: () => {
        this.background = this.game.add.sprite(0, 0, 'background')
        this.ticket = this.game.add.sprite(this.game.world.centerX, 130, 'ticket')
        this.ticket.anchor.setTo(0.5)
        this.claimButton = this.game.add.sprite(this.game.world.centerX + 180, 125, 'claimButton')
        this.claimButton.anchor.setTo(0.5);

        this.claimButton.inputEnabled = true;
        this.claimButton.input.pixelPerfectClick = true;
        //this.claimButton.events.onInputDown.add(function () { console.log('It worked as I expected') })
        this.claimButton.events.onInputDown.add(this.game.claimTicket,this)
    },
    claimTicket: (sprite) => {
        console.log('claimed')
    }

};

//initiate the Phaser framework
var game = new Phaser.Game(640, 360, Phaser.AUTO);

game.state.add('GameState', GameState);
game.state.start('GameState');

我希望从 this.claimButton.events.onInputDown.add() 内部调用函数 claimTicket 但它显示错误

因为this绑定了箭头函数

GameState.create里面的this是外层作用域(可能是全局的)


只是不要在这里使用箭头函数。

let GameState = {
    ...
    create(){
        ...
    },
    ...
}

let GameState = {
    ...
    create:function(){
        ...
    },
    ...
}