Jump to content

Fade out text after 2 second delay


owen
 Share

Recommended Posts

Hi this is a newbie question about displaying text.

 

I want to display some text at a certain position - which is easy enough.  The hard part is that after displaying, I want the text to travel 'up' the screen slowly and 'fade out'.  I want this 'move up and fade out' to occur over a timespan of 2 seconds.

 

The reason is that I want a 'score' to display when the player picks up some treasure, at the position of the treasure.  Then I want the score text to rise up, fade and disappear.

 

If I were doing this my old way (with hand-cranked css and html) then I would use css transitions & transforms but I'm a bit clueless when it comes to doing this with Phaser.  For a start how do I specify a time period (2 seconds) between one state and another.  Then how do I specify what change is to take place (fade out, travel upwards).

 

It's also important that I can have many of these things on screen at once. (For when player picks up multiple treasures).  Easy, right?

 

Well I'm pretty sure this is extremely easy but I can't find any examples.  I suspect it's to do with 'tweens' yes?

 

Thanks

Owen

Link to comment
Share on other sites

I believe this will do what you want

game.time.events.add(2000, function() {    game.add.tween(myText).to({y: 0}, 1500, Phaser.Easing.Linear.None, true);    game.add.tween(myText).to({alpha: 0}, 1500, Phaser.Easing.Linear.None, true);}, this);

What the code do:

-Wait 2 seconds

-Start 2 tweens:

    -One which will move the text to y=0 in 1500ms

    -One which will fade the text (from alpha = 1 to alpha = 0) in 1500ms too

 

You can change the Easing-Equation and the time it'll take to fade and move (here it's 1500).

Link to comment
Share on other sites

I believe this will do what you want

game.time.events.add(2000, function() {    game.add.tween(myText).to({y: 0}, 1500, Phaser.Easing.Linear.None, true);    game.add.tween(myText).to({alpha: 0}, 1500, Phaser.Easing.Linear.None, true);}, this);

What the code do:

-Wait 2 seconds

-Start 2 tweens:

    -One which will move the text to y=0 in 1500ms

    -One which will fade the text (from alpha = 1 to alpha = 0) in 1500ms too

 

You can change the Easing-Equation and the time it'll take to fade and move (here it's 1500).

 

 

That works great and I adjusted it slightly to suit my needs.  Awesome thank you.!!!

( where i used it -  http://www.owensouthwood.com/mrgoggles  )

Link to comment
Share on other sites

  • 1 month later...
  • 3 years later...
 Share

  • Recently Browsing   0 members

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