所以我正在尝试使用es6类进行移相器3。我的代码:
class Pcu extends Phaser.Scene {
constructor() {
super({key: 'Create',active: true});
this.bla = 0
}
preload() {}
create() {
this.bla = 1
}
}
module.exports = Pcu
和:
const Phaser = require('phaser')
const Pcu = require('./scenes/Pcu')
class Main extends Phaser.Game {
constructor() {
super({
type: Phaser.AUTO,
})
this.scene.add('Pcu', new Pcu(), false);
this.aa = new Pcu()
}
blabla() {
console.log(this.aa.bla)
}
}
module.exports = Main
现在我的问题是(考虑我的代码)如何在this.bla
中修改Main
后从create()
访问console.log(this.aa.bla + 1)
? (现在this.aa = new Pcu()
只返回0)
BTW有更好的方法来做Pcu()
吗?我的意思是现在就像我正在做this.scene.add('Pcu', new Pcu(), false);
两次。对?
在你的行,
Pcu
你传递的scene.add()
类的新实例没有引用到Pcu
函数。这意味着您将无法访问Pcu
实例。
我认为你想要的是这样的事情(扭转你的声明和使用this.aa = new Pcu();
this.scene.add('Pcu', this.aa, false);
实例):
console.log(this.aa.bla)
然后,当你打电话给1
时,你应该看到所需的结果:qazxswpoi。