christian.tucker Posted December 9, 2014 Report Share Posted December 9, 2014 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); Quote Link to comment Share on other sites More sharing options...
Taleforge Posted December 9, 2014 Report Share Posted December 9, 2014 this is because combatMoveBack will not be executed with the context set to "this" - it will be executed in its own context.To do so, your anonymous function has to look like this:function { combatMoveBack.call(this, damage);} christian.tucker 1 Quote Link to comment Share on other sites More sharing options...
Recommended Posts
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.