所以,我正在尝试用es6类来做Phaser3。我的代码:
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)只返回0)
顺便说一句,有没有更好的方法来做this.aa = new Pcu()?我的意思是,现在就像是我在做两次Pcu()。对吗?
发布于 2018-09-05 02:33:07
在你的队伍里,
this.scene.add('Pcu', new Pcu(), false);您正在传递一个没有引用到scene.add()函数的Pcu类的新实例。这意味着您将无法访问Pcu实例。
我认为您要做的事情是这样的(颠倒您对Pcu实例的声明和使用):
this.aa = new Pcu();
this.scene.add('Pcu', this.aa, false);然后,当您调用console.log(this.aa.bla)时,应该会看到所需的结果:1。
https://stackoverflow.com/questions/52171910
复制相似问题