无法从 Ionic2 中的移相器事件调用外部打字稿函数
unable to call external typescript function from phaser events in Ionic2
我正在研究 Ionic2 + Phaserjs。
我已经创建了 Phaser canvas 并在其中加载了 sprite。现在我想在精灵上添加拖动事件并在该拖动事件中调用打字稿函数。但我的问题是 Phaser 为自己的范围保留了 'this' 关键字,所以我不能像 this.sayHello().
这样调用外部打字稿函数
create() { // this is Phaser.create function
//console.log(vv)
this.counter = 0;
this.game.scale.scaleMode = Phaser.ScaleManager.SHOW_ALL;
this.game.stage.backgroundColor = "#4488AA";
console.log("hellow");
this.logo = this.game.add.sprite(this.game.world.centerX, this.game.world.centerY, 'logo');
this.logo.anchor.setTo(0.5, 0.5);
this.logo.scale.setTo(.5);
this.logo.inputEnabled = true;
this.logo.input.enableDrag();
// this.logo.events.onDragStart.add(this.openHyperlink(), this);
}
this.openHyperlink = function(){
console.log(this.mainApp)
}
当你声明你的打字稿函数时使用箭头符号
示例:
sayHello = () => { console.log('Hi');
this.something(); //here this is the original this object
};
由于 TypeScript 的箭头符号保留了原始对象
我正在研究 Ionic2 + Phaserjs。
我已经创建了 Phaser canvas 并在其中加载了 sprite。现在我想在精灵上添加拖动事件并在该拖动事件中调用打字稿函数。但我的问题是 Phaser 为自己的范围保留了 'this' 关键字,所以我不能像 this.sayHello().
这样调用外部打字稿函数create() { // this is Phaser.create function
//console.log(vv)
this.counter = 0;
this.game.scale.scaleMode = Phaser.ScaleManager.SHOW_ALL;
this.game.stage.backgroundColor = "#4488AA";
console.log("hellow");
this.logo = this.game.add.sprite(this.game.world.centerX, this.game.world.centerY, 'logo');
this.logo.anchor.setTo(0.5, 0.5);
this.logo.scale.setTo(.5);
this.logo.inputEnabled = true;
this.logo.input.enableDrag();
// this.logo.events.onDragStart.add(this.openHyperlink(), this);
}
this.openHyperlink = function(){
console.log(this.mainApp)
}
当你声明你的打字稿函数时使用箭头符号 示例:
sayHello = () => { console.log('Hi');
this.something(); //here this is the original this object
};
由于 TypeScript 的箭头符号保留了原始对象