Jump to content

Sprite persistence between states


darkraziel
 Share

Recommended Posts

I have a quite simple question for anyone who could help me. I've developed a quick tool I use in my games to manage achievements. Along with this tool I have a class that creates notifications that show up when you get a new achievement.

 

The way I show the notification is by using the code "this.game.add.sprite()". This notification enters the screen, stays for a few seconds and then leaves the screen by using tweens.

 

My problem is that sometimes I get an achievement just as the game is about to end, and when I change states (gameState -> gameOverState), the notification dissapears.

 

I wanted to know if there's any way to create and add a sprite that will persist between states. Maybe add it to a group that's on top of stages?

 

Any help would be appreciated.

 

ps: sorry for the (possibly) broken english, it's not my native language.

Link to comment
Share on other sites

 

By default Phaser adds all sprites to "world" and world is being cleared while states change. 
 
You should add sprite to stage instead of world. Here is pseudocode:
var img = game.make.image(0, 0, "blah", "bah");game.stage.addChild(img);

 

 

This will allow the notification to persist between states, but it introduces a problem, which is that tweens and timers do not persist between states, either. So this means that if the tween is initiated by a timer set when the notification first appears, and the state changes before the timer fires, the timer will never fire and the notification will never tween away. It also means that if the state changes while a tween is in progress, the notification will freeze in place and remain there permanently.

 

This is solvable, of course: at the beginning of the next state, you should use an if block to check if the notification is present in the stage, and if it is, create a new timer and a new tween. You'll probably also want to check if the notification is in its original x/y position or not, because it'll be somewhere else if it froze mid-tween, and in that case you'd want to fire the new tween immediately rather than waiting on a timer.

Link to comment
Share on other sites

This will allow the notification to persist between states, but it introduces a problem, which is that tweens and timers do not persist between states, either. So this means that if the tween is initiated by a timer set when the notification first appears, and the state changes before the timer fires, the timer will never fire and the notification will never tween away. It also means that if the state changes while a tween is in progress, the notification will freeze in place and remain there permanently.

 

This is solvable, of course: at the beginning of the next state, you should use an if block to check if the notification is present in the stage, and if it is, create a new timer and a new tween. You'll probably also want to check if the notification is in its original x/y position or not, because it'll be somewhere else if it froze mid-tween, and in that case you'd want to fire the new tween immediately rather than waiting on a timer.

Thank you very much. After some changes to my project I could easily implement all this changes.

Link to comment
Share on other sites

 Share

  • Recently Browsing   0 members

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