【问题标题】:Phaser: Tween in different functions sets object to undefinedPhaser:不同函数中的补间将对象设置为未定义
【发布时间】:2017-06-14 11:54:57
【问题描述】:

我在函数调用中添加了一些精灵,如下所示:

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 Uncaught TypeError: Cannot read property 'x' of 未定义

指向this.comboBasemoveSlotMachineOut()。同样奇怪的是,上述函数中的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';

其余的有些相似。据我所知,我没有清除变量的值,所以我不确定发生了什么。

什么可能导致变量未定义?补间有什么作用吗?

【问题讨论】:

    标签: javascript phaser-framework


    【解决方案1】:

    显然,左括号和右括号可能意味着所有不同。

    改变这个:

    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);
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2017-04-19
      • 2014-10-06
      • 2017-09-19
      • 2015-07-23
      • 2017-07-29
      • 1970-01-01
      • 2022-01-23
      相关资源
      最近更新 更多