我在函数调用中补间了一些 Sprite ,如下所示:
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);
}
这是可行的,并且在函数调用时, Sprite 会从左向右滑入。
现在,我还应该将对象滑出。这是通过计时器完成的,因此它不会立即滑出,如下所示:
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 Uncaught TypeError: Cannot read property 'x' of undefined
它指向moveSlotMachineOut()
的this.comboBase
。同样奇怪的是,该函数中的 console.log
位返回 undefined。
这是我对 this.comboBase
的初始化:
this.comboBase = this.game.add.sprite(this.x -10, this.y, 'ui');
this.comboBase.anchor.set(0.5, 0.5);
this.comboBase.frameName = 'ui_specialBase.png';
其余部分有些相似。据我所知,我没有清除变量的值,所以我不确定发生了什么。
什么可能导致变量未定义?补间有什么作用吗?
最佳答案
显然,左括号和右括号可能意味着完全不同。
改变这个:
this.game.time.events.add(3000, this.comboLayer.moveSlotMachineOut, this);
对此:
this.game.time.events.add(3000, this.comboLayer.moveSlotMachineOut(), this);
现在这会产生一个完全不同的错误,因为(据我所知).add
不喜欢带括号的任何内容,所以最终的代码是:
this.game.time.events.add(3000, function() {this.comboLayer.moveSlotMachineOut()}, this);
关于javascript - 移相器 : Tween in different functions sets object to undefined,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44520969/