Jump to content

Passing arguments to a onComplete callback causes errors.


christian.tucker
 Share

Recommended Posts

Working code:

Player.prototype.attack = function(target, damage) {    game.world.bringToTop(this.playerSprite);    var movementTween = game.add.tween(this.playerSprite.body).to( { x: target.getTile().getX() + ((this.team == 1) ? -30 : 30), y: target.getTile().getY() }, 1500, Phaser.Easing.Linear.None, true);    movementTween.onComplete.add(combatMoveBack, this);    this.playerSprite.animations.play('walk');};function combatMoveBack() {    var movementTween = game.add.tween(this.playerSprite.body).to( { x: this.homeTile.getX(), y: this.homeTile.getY() }, 1500, Phaser.Easing.Linear.None, true);    this.playerSprite.scale.setTo(-1, 1);    movementTween.onComplete.add(combatStop, this);}

Not working code:

Player.prototype.attack = function(target, damage) {    game.world.bringToTop(this.playerSprite);    var movementTween = game.add.tween(this.playerSprite.body).to( { x: target.getTile().getX() + ((this.team == 1) ? -30 : 30), y: target.getTile().getY() }, 1500, Phaser.Easing.Linear.None, true);    movementTween.onComplete.add(function() { combatMoveBack(damage) }, this);    this.playerSprite.animations.play('walk');};function combatMoveBack(damage) {    console.log("Damage: " + damage);    var movementTween = game.add.tween(this.playerSprite.body).to( { x: this.homeTile.getX(), y: this.homeTile.getY() }, 1500, Phaser.Easing.Linear.None, true);    this.playerSprite.scale.setTo(-1, 1);    movementTween.onComplete.add(combatStop, this);}

When I use the anonymous function to call the combatMoveBack and apply the damage, the console.log reads the damage just fine, but then the tween can't access playerSprite.body

 

 

 Uncaught TypeError: Cannot read property 'body' of undefined

 

Line of code throwing error:

var movementTween = game.add.tween(this.playerSprite.body).to( { x: this.homeTile.getX(), y: this.homeTile.getY() }, 1500, Phaser.Easing.Linear.None, true);
Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...