Ready to watch this entire course?
Become a member and get unlimited access to the entire skills library of over 4,900 courses, including more Developer and personalized recommendations.Start Your Free Trial Now
- View Offline
Starting with installation and configuration of Starling and creation of a Flash Builder project, the course shows how to prepare and import graphical assets, create MovieClip classes from sprite sheets, manage various gaming objects, detect collisions, and add both particle and sound effects. The finished game can be deployed to any browser or mobile device that integrates Flash Player 11, which was released in September 2011.
- Downloading and installing Starling and Flash Builder
- Creating the project
- Creating the main game class, static assets class, and other classes
- Building the state machine
- Creating a static assets class
- Adding images
- Creating sprite sheets with Texture Packer
- Creating and importing bitmap fonts
- Detecting collisions
- Adding particle effects
- Adding sound effects
Skill Level Intermediate
Okay, so now we have our three main game states created. Now our main Game class is going to actually incorporate a state machine and its whole purpose is going to be to switch between the various states of our game. So with the top of Game class, I am going to define three constants which are actually going to represent each of the states in the state machine. So I am going to say public, and these are going to be static so that we can reference them anywhere and they are going to be directly attached to our class, and we are going to say const for constant, and the first one is going to be MENU_STATE and these are going to be simple integer values, and they just have to contain unique integer values.
I am going to set this one equal to zero. I am going to duplicate that twice and now we are going to change this to PLAY_STATE and I am going to make this one a value of 1. We'll make this one a value of 2 and change it to GAME_OVER_STATE, like that. So again, just these three constants and this is going to enable us to reference these states via a nice constant value instead of having to use the actual integer values themselves.
Now another property we are going to create up here is going to be variable which is going to hold what is the current state which is actually visible in our game. So we are going to create this an instance variable. So private var current_state and now this going to be of type IState, which is our interface. So it's nice that we can do this, because again, we don't have to reference them as their individual types, like GameOver, Menu or Play, since they all implement that IState interface, we can nicely type this to IState and it will essentially include all three of those.
Okay, so now we have that created, now what we need to do is to create a function, and this is going to be public function which is going to allow us to change the state of our game from any of the individual states. So let's say I'm currently in the Play state of my game and I die, and then I need to change the state of the game to GameOver. Well I am actually going to call this function. So we are going to create a public function and it's going to be called changeState, and to this function we are going to send in an integer value which represents which state we want to change to and it could be any one of these three constants.
So this is going to be of type int and we are not going to be returning anything here. So now inside of this changeState we are going to essentially use a switch statement to find out which state is actually passed in and then we are going use that value to change the state of our game. But what we first want to do is to come in because when we first come to our game, remember there is no current state. So this current_state variable will actually be null. So we are going to test for that at the very beginning of our changeState function.
So we are going to say if current_state not equal to null, that means we already have a state which is visible, and what we are going to do then is we are going to call the destroy method on the current_state. Let's say we are in the Menu state currently and we want to switch to the Play state. Well before we switch to the Play state we first want to destroy the Menu state and we do that by saying current_state.destroy. And here again, why it's important that all of the states all have that destroy method implemented.
And just to make sure that the garbage collector does its thing, we are going to set current_state here equal to null. So once it's destroyed, we are going to set it to null and then now at this point we can actually go ahead and reset it to the new state that we want to switch to. Okay, so with that if statement completed, now we can create our switch statement. So switch, and we are going to be switching over that state argument which was sent in, and now we are going to put in a case statement for each of the three possible states that we can switch to.
So we'll say case and the first one is going to be MENU_STATE, so we say it like that, case MENU_STATE, and if that is the state that we want to switch to we are going to set current_state equal to a new instance of the Menu state class. And remember when we created all of our state classes it's expecting us to send in a reference to this game class. We can do that just by saying this. So if that was the case, we are going to set that current_state property and then we are simply going to break.
Okay, so I am going to copy this code, this is going to be very similar for the other two cases, and this is one is going to be the PLAY_STATE and obviously here I am not creating a new instance of menu, I am creating a new instance of the Play state. I am going to need to make sure that we actually import that, because again they are in a different package. And lastly, there is going to be the GAME_OVER_STATE and for this we are going to create a new instance of the GameOver Class, like that.
So now once we go through that switch statement we'll now have set that current_state property equal to the correct state class, but what we need to do next is to actually add that state class to our display list here. So I am going to come underneath the switch statement and I am going to say addChild(current_state). Now we can't just keep it like this, because this is of type IState right now, so we need to make sure we cast this to a Sprite and we are just going to wrap it in a cast statement like this, so the display list knows that this is actually a valid Sprite class.
Okay, so now we have our changeState function implemented, what we need to do next is when we first come into our movie, in this init Event Handler here, right before we set the ENTER_FRAME event, we are going to want to change the initial state to be the Menu state. So we are going to call that ChangeState method and we are going to pass in MENU_STATE, because remember when we first launch our game we want to initially go to that MENU_STATE. So now I mentioned when we created this game class that we have our main Game loop which is this update function, this is being called on every frame.
So all we are going to do in this update method is to call the update method of our current state, because again, depending on what state we are in, we are going to be doing different things on every frame. So we are simply going to pass this call down to whatever state is currently active. So all we have to say here is current_state.update(). Okay, so just to recap again, we've created our three constants for the three different states and we've created our changeState method which looks at the integer which is passed in and chooses the correct state, creates a new instance of it and adds it to the display list.
And again, in our main game loop, we are simply calling the update method of whatever state is currently active.