Javascript Signal:listener是add()的必需参数,应该是函数
我目前正在使用Phaser 2.x。问题在于Javascript Signal:listener是add()的必需参数,应该是函数,javascript,phaser-framework,Javascript,Phaser Framework,我目前正在使用Phaser 2.x。问题在于create方法。我将函数作为参数传递给this.claimButton.events.onInputDown.add()方法。但它无法引用我当前所指的函数。对此我能做些什么 我已尝试使用this.game.claimTicket()和this.game.claimTicket并尝试将this.claimTicket作为参数放入this.claimButton.events.onInputDown.add()。但是我仍然得到一个错误,说uncaught
create
方法。我将函数作为参数传递给this.claimButton.events.onInputDown.add()
方法。但它无法引用我当前所指的函数。对此我能做些什么
我已尝试使用this.game.claimTicket()
和this.game.claimTicket
并尝试将this.claimTicket
作为参数放入this.claimButton.events.onInputDown.add()。但是我仍然得到一个错误,说uncaughterror:Phaser.Signal:listener是add()的必需参数,应该是一个函数。
但是当我尝试this.claimButton.events.onInputDown.add(函数(){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
,但它显示了错误,这是因为此
绑定了arrow函数
GameState.create中的这个是外部范围(可能是全局范围)
只是不要在这里使用箭头函数
let GameState = {
...
create(){
...
},
...
}
或
非常感谢你!它解决了我的问题。再次感谢。真的很有帮助
let GameState = {
...
create:function(){
...
},
...
}