Easy-to-follow video tutorials help you learn software, creative, and business skills.Become a member
So now that we have our IState interface created, we can go ahead and create those three Game states: the Menu state, the Play state and the Game Over state. So I'm going to right-click on our source folder and choose New > ActionScript Class, and for a Package we are going to store these state classes in a package called states. And the first one I'm going to create is the Menu state. Now for a superclass this is going to subclass starling.display.Sprite, because it's going to contain the actual visual representation for the state.
And for interfaces we are going to Add that IState interface, so if we actually just search for it here we can see the IState interface, click OK, and now we can click Finish. And you could see automatically by implementing that interface it's now created the update and destroy methods for us. So we are going to make a couple of changes to this. The first thing we are going to do is when we create one of these states from our Game class, we are actually going to send in a reference to our main Game class so that we can communicate back to it.
So in the constructor here, I'm going to put an argument called game, and this is going to be of type Game. And now inside of our constructor we are going to create an instance property called game, so I'm going to say this.game = game; and now I can hit Command+1 or Ctrl+1 to get a quick fix to create that as an instance variable at the top. So now anywhere inside of this state I can actually call back to our main Game class. Now the next thing I'm going to do is to add an event listener for the added to stage event, again because we don't want to do anything here until we know it's been added to the stage.
So I'm going say addEventListener and again be sure when you are importing event that you are importing starling.events.Event. We are going to listen for ADDED_TO_ STAGE and we are going to have it call a function called init. And let's go ahead and define that function now. So private function init, we are going to have our event object be of type starling.events.Event. I'm not going to return anything and now we have that set up. So this is going to be main template that each of our states is going to implement.
So what I'm going to do is copy this entire class to the clipboard, and now I'm going to right-click in the states package and we are going to create the other two states. So New > ActionScript Class, and this one is going to be called Play, and again, the Superclass is going to be starling.display.Sprite. For interfaces we are going to implement that IState interface and click Finish. Now I'm just simply going to select all of text in here and paste in that Menu state, because now all we need to do is to simply change Menu to Play here and there, and now we have our Play state created.
So now we are going to do the same thing for the Game Over state. So New > ActionScript Class, this one is called GameOver, Superclass is going to be Sprite. We are going to implement the IState interface, click Finish, and again, replace the text with the text that's on the clipboard, change this to GameOver and GameOver like that. So now you can see we have our states package and we have our three states created.
So now in the next movie we are going to look at implementing a state machine which is going to control the switching of the various states of our game.
Get unlimited access to all courses for just $25/month.Become a member
82 Video lessons · 97140 Viewers
61 Video lessons · 84431 Viewers
71 Video lessons · 68637 Viewers
56 Video lessons · 101128 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.