zack_falcon
zack_falcon

Reputation: 4376

Phaser: Tween in different functions sets object to undefined

I've got some sprites that I tween in on a function call, like so:

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 works, and upon function call, the sprites do slide in from the left, to the right.

Now, I'm also supposed to slide the objects out. This is done via timer, so that it doesn't immediately slide out, like so:

this.game.time.events.add(3000, this.comboLayer.moveSlotMachineOut, this);

That calls this function:

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

But for some reason, I get the following error:

phaser.js:64795 Uncaught TypeError: Cannot read property 'x' of undefined

Which is pointing to this.comboBase of moveSlotMachineOut(). What is also weird is that the console.log bit in said function returns undefined.

Here's my initialization of 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';

The rest of them are somewhat similar. To my knowledge, I do not clear the values of the variables, so I'm not sure what's happening.

What could be causing the variables to be undefined? Does tweening do anything?

Upvotes: 0

Views: 331

Answers (1)

zack_falcon
zack_falcon

Reputation: 4376

Apparently, open and close parenthesis can mean all the difference.

Changing this:

this.game.time.events.add(3000, this.comboLayer.moveSlotMachineOut, this);

To this:

this.game.time.events.add(3000, this.comboLayer.moveSlotMachineOut(), this);

Now that would create an entirely different error, as (AFAIK) .add doesn't like anything with parenthesis, so the final code is:

this.game.time.events.add(3000, function() {this.comboLayer.moveSlotMachineOut()}, this);

Upvotes: 1

Related Questions