Easy-to-follow video tutorials help you learn software, creative, and business skills.Become a member
So now, we have our animating background in our game over text. Last thing we need to do in the GameOver state is to create our Try Again button. So I am going to create a property here called tryAgain. I'm going to set that equal to a new Starling button instance, and now we need to get that texture from our texture atlas. So I am going to say Assets, make sure we import that class .ta.getTexture and the texture name is tryAgainButton.
Now let's go ahead, and create that as an instance variable. Now we want to set the pivot X property of the button. That way we can easily position it in the center of our movie. So we are going to say tryAgain.pivotX = tryAgain.width * 0.5. Let's position this now. So we are going to say tryAgain.x. You can put it right in the center at 400, tryAgain.y = 450.
Again, that's just a trial and error thing to position it correctly. Now let's add it to the display list; so addChild (tryAgain). Now much like we did in the menu state, we need to listen for the triggered event on this button so that we know when to actually let the user play the game again. So I am going to come right under where we created it and say, tryAgain.addEventListener. We want Event. Again, make sure you get starling. events.Event, and now we want the TRIGGERED event, and we'll just call the event handler onAgain.
So let me create that method, and make sure we have our event object being caught here. And in this onAgain event, what we are going to do is to change the game state to play. So again, we're going to say game.changeState, and then we are going to get that static state property of PLAY_STATE. So now let's go ahead and test it, and make sure it works. So we'll debug here, click Play Game, and I'll let myself die.
We are in the GameOver state, and we can see our button being displayed, and when I click it, we actually go back to the Play state. Okay, so now we just need to implement the destroy method here, and to do that, we are essentially just going to removeFromParent(true). So we are going to make sure we dispose that. Now one of the things that's good practice to do is since we are listening for this event here, let's go ahead and unregister for that event here.
We'll say tryAgain.removeEventListener. So again, my philosophy on cleaning up after yourself, it's better to do more. Even if some things aren't entirely necessary, it's just good practice to get into. So now let's go ahead and test it. Again, we come in, we have our game, we die, GameOver state; TRY AGAIN brings us back to the play state so we continue playing our game. And again, I just wanted to highlight to you that the performance that you are seeing here is not necessarily accurate of the real performance of the game.
For numerous reasons, this is a debug build, and I mentioned before how with Starling in particular, you really need to do a release build of your game in order to get an idea of the real performance, because you can see even though the frame rate here is 60 frames per second, it still looks a little stuttery, and that's because we are looking at this using a debug build and also in the debug player. But that's the GameOver state, and in the next chapter, we are actually going to focus on creating some more visual effects.
We are going to create particles so that when you actually shoot a bullet and it hits an alien, we are going to see an explosion particle so we'll add a little more eye candy to our game in the next chapter.
Get unlimited access to all courses for just $25/month.Become a member