Easy-to-follow video tutorials help you learn software, creative, and business skills.Become a member
Now that we have our background, we are going to focus on creating the game over text, and we are going to use a Starling text field to display that. So I'm going to come under where I created my background in my init method. I'm going to create a variable here, and I'm going to call it overText, and I'm going to set that equal to a new text field. And again, be sure you are using starling.text.TextField. And to that, we are going to pass in a few different things; first, the width and the height.
I'm going to put the width at 800, so it's the whole width of my game and we are going to set its align to Center, so that way, it's centered in the middle. And I'm going to set the height here to 200, pass in what actual texturing we want to display, and we can set all of these also outside of the constructor, but I'm just doing it here. We are going to say GAME OVER. Now we need to use uppercase, because remember, when I created that bitmap font, when we did the assets chapter, I only included the glyphs for the uppercase characters because that's all the font actually contains.
Now next is the font name. So remember, in our assets class, we have this Komika font. So if I open that with the Text Editor here, remember this is the name that I need to give when I want to use that font. So I'm going to give it a string of KomikaAxis like that. Now next is the size of the font. I'm going to do 72 and then the color, and I'm going to do white. I'm going to create that as an instance variable.
Now I want to set up a couple of things with it. First, I want to set its align property. So I'm going to set overText and we want to set the hAlign property, and we just set it to a string of center, and now I'm going to set the y position of that. So overText.y, because remember, it will automatically be centered on the x axis. I'm going to set it to 200, and then lastly, we want to add this text to the display list. So addChild(overText); like that.
Let's see what that looks like. So we will launch our game, and we'll die on purpose. And there we have our text nicely centered and we are using that bitmap font. So again, this is a nice way to be able to render text, and we don't have to include a PNG file that says game over. If we are using a lot of text in our game, we can just reference those bitmap fonts, and use the text field class that is included with Starling. So the next step is to add our Try Again button which the user can click on to try again at playing the game.
Get unlimited access to all courses for just $25/month.Become a member
82 Video lessons · 97118 Viewers
61 Video lessons · 84417 Viewers
71 Video lessons · 68626 Viewers
56 Video lessons · 101113 Viewers
Access exercise files from a button right under the course name.
Search within course videos and transcripts, and jump right to the results.
Remove icons showing you already watched videos if you want to start over.
Make the video wide, narrow, full-screen, or pop the player out of the page into its own window.
Click on text in the transcript to jump to that spot in the video. As the video plays, the relevant spot in the transcript will be highlighted.
Your file was successfully uploaded.