pranadevil Posted June 21, 2016 Share Posted June 21, 2016 hi all, im wondering how can i tween a numeric value lets say. var number1; /// in create function number1 = 100; ///tween from 100 to 10 and show it by a text text1.setText( " from 100 to 10: " + number1+ " "); its possible? im chasing a way to show player his score after complete world. but i wanna show him the time bonus and move the time left to zero on screen. any suggestion? Link to comment Share on other sites More sharing options...
end3r Posted June 21, 2016 Share Posted June 21, 2016 Yes, tweening a score is totally possible. Check out my Enclave Phaser Template - there's a Game.js file with gameOverScreen function, or see this commit in particular: gameoverScoreTween: function() { this.screenGameoverScore.setText('Score: 0'); if(this._score) { this.tweenedPoints = 0; var pointsTween = this.add.tween(this); pointsTween.to({ tweenedPoints: this._score }, 1000, Phaser.Easing.Linear.None, true, 500); pointsTween.onUpdateCallback(function(){ this.screenGameoverScore.setText('Score: '+Math.floor(this.tweenedPoints)); }, this); pointsTween.onComplete.addOnce(function(){ this.screenGameoverScore.setText('Score: '+this._score); }, this); pointsTween.start(); } }, It's good to remember that when tweening the score you can hook up to onUpdateCallback to update the score text during tween. pranadevil and Xarialon 2 Link to comment Share on other sites More sharing options...
pranadevil Posted June 21, 2016 Author Share Posted June 21, 2016 1 hour ago, end3r said: Yes, tweening a score is totally possible. Check out my Enclave Phaser Template - there's a Game.js file with gameOverScreen function, or see this commit in particular: gameoverScoreTween: function() { this.screenGameoverScore.setText('Score: 0'); if(this._score) { this.tweenedPoints = 0; var pointsTween = this.add.tween(this); pointsTween.to({ tweenedPoints: this._score }, 1000, Phaser.Easing.Linear.None, true, 500); pointsTween.onUpdateCallback(function(){ this.screenGameoverScore.setText('Score: '+Math.floor(this.tweenedPoints)); }, this); pointsTween.onComplete.addOnce(function(){ this.screenGameoverScore.setText('Score: '+this._score); }, this); pointsTween.start(); } }, It's good to remember that when tweening the score you can hook up to onUpdateCallback to update the score text during tween. was exactly what i was looking for, thanks! Link to comment Share on other sites More sharing options...
pranadevil Posted June 21, 2016 Author Share Posted June 21, 2016 i tried it but looks like when i start from 0 to for example 10, the text goes something like: 1,2,3,4.....9.98888,9.998888....and so on why it never ends? Link to comment Share on other sites More sharing options...
end3r Posted June 21, 2016 Share Posted June 21, 2016 I suppose it depends on the type of tween you used - in my case it was linear, so I didn't have such problems, I also used Math.floor to show only the integers. Link to comment Share on other sites More sharing options...
pranadevil Posted June 21, 2016 Author Share Posted June 21, 2016 8 minutes ago, end3r said: I suppose it depends on the type of tween you used - in my case it was linear, so I didn't have such problems, I also used Math.floor to show only the integers. Very curious cause i also used linear. So did u tested yours and it reaches the final value? Did u used a function outside update function to start the tween right? Link to comment Share on other sites More sharing options...
end3r Posted June 22, 2016 Share Posted June 22, 2016 There were some issues because at the end of the tween (onComplete) I'm replacing the current value with the original, final one. And yes, it's starting in the stateGameover function launched once. Link to comment Share on other sites More sharing options...
Recommended Posts