Javascript 相位器:不同功能中的吐温将对象设置为未定义
我在函数调用中加入了一些精灵,如下所示:Javascript 相位器:不同功能中的吐温将对象设置为未定义,javascript,phaser-framework,Javascript,Phaser Framework,我在函数调用中加入了一些精灵,如下所示: moveSlotMachineIn() { var comboBaseTweenIn = this.game.add.tween(this.comboBase).to({x: 10}, 2000, "Quart.easeOut", 3000); var comboTopTweenIn = this.game.add.tween(this.comboTop).to({x: 10}, 2000, "Quart.easeOut", 3000);
moveSlotMachineIn() {
var comboBaseTweenIn = this.game.add.tween(this.comboBase).to({x: 10}, 2000, "Quart.easeOut", 3000);
var comboTopTweenIn = this.game.add.tween(this.comboTop).to({x: 10}, 2000, "Quart.easeOut", 3000);
var comboMaskTweenIn = this.game.add.tween(this.comboMask).to({x: 10}, 2000, "Quart.easeOut", 3000);
var arrowGrpTweenIn = this.game.add.tween(this.arrowSlotGroup).to({x: 200}, 2000, "Quart.easeOut", 3000);
}
this.game.time.events.add(3000, this.comboLayer.moveSlotMachineOut, this);
这是可行的,在函数调用时,精灵会从左向右滑动
现在,我还要把物体滑出来。这是通过定时器完成的,因此它不会立即滑出,如下所示:
moveSlotMachineIn() {
var comboBaseTweenIn = this.game.add.tween(this.comboBase).to({x: 10}, 2000, "Quart.easeOut", 3000);
var comboTopTweenIn = this.game.add.tween(this.comboTop).to({x: 10}, 2000, "Quart.easeOut", 3000);
var comboMaskTweenIn = this.game.add.tween(this.comboMask).to({x: 10}, 2000, "Quart.easeOut", 3000);
var arrowGrpTweenIn = this.game.add.tween(this.arrowSlotGroup).to({x: 200}, 2000, "Quart.easeOut", 3000);
}
this.game.time.events.add(3000, this.comboLayer.moveSlotMachineOut, this);
调用此函数的:
moveSlotMachineOut() {
console.log(this.comboBase);
var comboBaseTweenOut = this.game.add.tween(this.comboBase).to({x: 1600}, 2000, "Quart.easeOut", 3000);
var comboTopTweenOut = this.game.add.tween(this.comboTop).to({x: 1600}, 2000, "Quart.easeOut", 3000);
var comboMaskTweenOut = this.game.add.tween(this.comboMask).to({x: 1600}, 2000, "Quart.easeOut", 3000);
var arrowGrpTweenOut = this.game.add.tween(this.arrowSlotGroup).to({x: 1600}, 2000, "Quart.easeOut", 3000);
}
但由于某种原因,我得到了以下错误:
phaser.js:64795未捕获类型错误:无法读取的属性“x”
未定义
它指向moveSlotMachineOut的这个.comboBase。同样奇怪的是,所述函数中的console.log位返回undefined
下面是我对这个.comboBase的初始化:
其余的都有些相似。据我所知,我不清楚变量的值,所以我不确定发生了什么
是什么导致变量未定义?tweening有什么作用吗?显然,开括号和闭括号可能意味着所有的区别 更改此选项:
this.game.time.events.add(3000, this.comboLayer.moveSlotMachineOut, this);
为此:
this.game.time.events.add(3000, this.comboLayer.moveSlotMachineOut(), this);
现在,这将创建一个完全不同的错误,因为AFAIK.add不喜欢任何带括号的内容,因此最终代码是:
this.game.time.events.add(3000, function() {this.comboLayer.moveSlotMachineOut()}, this);