Easy-to-follow video tutorials help you learn software, creative, and business skills.Become a member
So the last thing we are going to add to our game is just a simple high score text field. And what I'm going to do is create a new object class. I am going to go to my objects package, say New > ActionScript Class, and I am going to call it Score. Now I am just going to subclass Sprite. Now if you only literally wanted a text field, you could in subclass text field. But I am just using this because, generally speaking, you might want to add more to it in the future, and if so, you need to display object container. So I am going to subclass starling. display.Sprite, and in the constructor here I am going to create a new variable called score, and that's going to be equal to a new text field object, a new starling.text.TextField.
And to the constructor of this I am going to give it a width of 300, height of 100. I am going to give it an initial text value of 0, because we don't have any score when we come in. I am going to use the KomikaAxis font. That's our bitmap font that we included. I am going to give it a size of 32 and a color of white. And let's create score as an instance variable in our class here. So now what I am going to do is to align the text, the horizontal line to write, so I am going to say score.hAlign = "right", and I want to add this score text field to the display list, like that.
So now I am going to create a public function that we are going to call from anywhere in our game to essentially add a number to our score. And I am just going to call it addScore and we are going to send in an amount variable which will just be a number, just in case there is a decimal value. We are not going to return anything here, and what we are simply going to do is to reset the score text property equal to, and then I am going to open some parentheses here. I am going to parseInt the current value which is in there, score.text, and I'm going to add the amount that was sent in.
So you are going to imagine maybe there would be a negative number if we did something wrong in the game. I'm going to close that parenthesis and then just do a toString value here to turn it back into a string. And that will successfully change that text field's text based on the number. Okay, so now what I want to do is to go into my CollisionManager class and every time a bullet hits an alien we want to add an amount to that. So what I am going to do is to say play.score and we haven't actually created this property yet in our play state, and we are going to call addScore, and we are going to add 200 to the score every time a bullet hits an alien.
And now let's go to our play state, I am going to come into the init function, let's just do it after we create our hero, and I am going to create that score variable is equal to new score, and I want to create that as an instance variable and this needs to be public because again we are referencing it from the CollisionManager class and let's add this to the display list. So addChild(score) and let's set its X property, score.x = 450, like that.
So again in our CollisionManager we are going to add 200 to the score every time a bullet hits one of the aliens. So let's test that now. We'll come in, we can see our score in the upper right. And every time I successfully shoot one of the aliens you can see my score is going up by 200. So again we took a really simple approach to a high score read out, again you are going to want to do things as your game gets more complicated, you may want to subtract scores and all different types of things could happen.
But that's a general approach in creating a score object and providing methods to allow you to add or subtract from the high score.
Get unlimited access to all courses for just $25/month.Become a member
82 Video lessons · 101431 Viewers
61 Video lessons · 88193 Viewers
71 Video lessons · 72061 Viewers
56 Video lessons · 103837 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.