2015-04-27 91 views
1

我在移相器中創建一個按鈕:移相器按鈕不調用函數

//---------------------------------Boot 
module.exports = { 

init: function() { 
    closeButton = game.add.button(w/1.47, h/1.272, 'close', this.closeProf, this); 
    closeButton.fixedToCamera = true; 
    closeButton.inputEnabled = true; 
}, 

closeProf: function() { 
    alert('aaa') 
} 
}; 

Module.exports - 這是Browserify。

在主文件中有這樣的代碼:

//---------------------------------Main 
window.game = new Phaser.Game((h > w) ? h : w, (h > w) ? w : h, Phaser.CANVAS, 'game', {render:render}); 

var ship; 
var cursors; 
var sun1; 
var rc; 
var space; 

var profile; 
var closeButton, holdLeft, holdRight, avaProfile; 

game.state.add('Boot', require('./states/boot.js')); 
game.state.add('Play', require('./states/play.js')); 
game.state.start('Boot'); 

如何使按鈕調用一個函數?

回答

0

看起來它本身就是closeProf。在創建按鈕之前,添加一個變量var that = this。 不要添加上下文,這意味着您要撥打this.this.closeProf

按鈕代碼應該是這樣的:

module.exports = { 

    init: function() { 
    var that = this; 
    closeButton = game.add.button(w/1.47, h/1.272, 'close', that.closeProf); 
    closeButton.fixedToCamera = true; 
    closeButton.inputEnabled = true; 
}, 

closeProf: function() { 
    alert('aaa') 
} 
};