Our website will be down for scheduled maintenance on Thursday, September 18, at 11 p.m. Pacific Time and should be back up by Midnight. We apologize for any inconvenience.

Easy-to-follow video tutorials help you learn software, creative, and business skills.Become a member

Building the state machine

From: Building Flash Games with Starling

Video: Building the state machine

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.

Building the state machine

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.

Show transcript

This video is part of

Image for Building Flash Games with Starling
Building Flash Games with Starling

46 video lessons · 9517 viewers

Lee Brimelow
Author

 
Expand all | Collapse all
  1. 2m 9s
    1. Welcome
      45s
    2. Using the exercise files
      36s
    3. What you should know
      48s
  2. 10m 21s
    1. Checking out the finished product
      1m 10s
    2. Downloading the Starling framework
      4m 13s
    3. Installing Flash Builder
      1m 44s
    4. Installing additional tools
      1m 30s
    5. Overview of the Starling framework
      1m 44s
  3. 13m 0s
    1. Creating the Flash Builder project
      2m 40s
    2. Setting up your Starling scene
      6m 7s
    3. Creating the main game class
      4m 13s
  4. 13m 52s
    1. Creating the IState interface
      2m 14s
    2. Creating the three game states
      3m 50s
    3. Building the state machine
      7m 48s
  5. 22m 14s
    1. Creating a static assets class
      2m 3s
    2. Adding images and creating textures
      3m 24s
    3. Creating sprite sheets with TexturePacker
      6m 26s
    4. Creating the main texture atlas
      3m 29s
    5. Creating and importing bitmap fonts
      6m 52s
  6. 14m 16s
    1. Creating the Background class
      5m 55s
    2. Adding the logo
      2m 58s
    3. Adding the play button
      3m 4s
    4. Implementing the destroy method
      2m 19s
  7. 56m 47s
    1. Adding the background
      1m 3s
    2. Creating the Hero class
      6m 14s
    3. Creating the Bullet class
      1m 44s
    4. Creating the BulletManager class
      4m 23s
    5. Using the StarlingPool class
      9m 24s
    6. Firing bullets with the mouse
      7m 24s
    7. Creating the Alien movie clip
      1m 58s
    8. Creating the AlienManager class
      8m 12s
    9. Overview of collision-detection options
      3m 10s
    10. Creating the CollisionManager class
      10m 51s
    11. Implementing the destroy method
      2m 24s
  8. 7m 59s
    1. Adding the background
      1m 10s
    2. Creating the "Game Over" text
      2m 43s
    3. Adding the Try Again button
      4m 6s
  9. 20m 5s
    1. Exploring particle-creation tools
      4m 52s
    2. Adding the smoke texture
      5m 3s
    3. Creating the Explosion class
      2m 6s
    4. Creating the ExplosionManager class
      8m 4s
  10. 11m 5s
    1. Creating sound effects with cfxr
      3m 5s
    2. Adding the sound effects
      3m 42s
    3. Creating the Score class
      4m 18s
  11. 6m 1s
    1. Starling optimization tips
      4m 8s
    2. Helpful Starling resources
      1m 53s

Start learning today

Get unlimited access to all courses for just $25/month.

Become a member
Sometimes @lynda teaches me how to use a program and sometimes Lynda.com changes my life forever. @JosefShutter
@lynda lynda.com is an absolute life saver when it comes to learning todays software. Definitely recommend it! #higherlearning @Michael_Caraway
@lynda The best thing online! Your database of courses is great! To the mark and very helpful. Thanks! @ru22more
Got to create something yesterday I never thought I could do. #thanks @lynda @Ngventurella
I really do love @lynda as a learning platform. Never stop learning and developing, it’s probably our greatest gift as a species! @soundslikedavid
@lynda just subscribed to lynda.com all I can say its brilliant join now trust me @ButchSamurai
@lynda is an awesome resource. The membership is priceless if you take advantage of it. @diabetic_techie
One of the best decision I made this year. Buy a 1yr subscription to @lynda @cybercaptive
guys lynda.com (@lynda) is the best. So far I’ve learned Java, principles of OO programming, and now learning about MS project @lucasmitchell
Signed back up to @lynda dot com. I’ve missed it!! Proper geeking out right now! #timetolearn #geek @JayGodbold
Share a link to this course

What are exercise files?

Exercise files are the same files the author uses in the course. Save time by downloading the author's files instead of setting up your own files, and learn by following along with the instructor.

Can I take this course without the exercise files?

Yes! If you decide you would like the exercise files later, you can upgrade to a premium account any time.

Become a member Download sample files See plans and pricing

Please wait... please wait ...
Upgrade to get access to exercise files.

Exercise files video

How to use exercise files.

Learn by watching, listening, and doing, Exercise files are the same files the author uses in the course, so you can download them and follow along Premium memberships include access to all exercise files in the library.


Exercise files

Exercise files video

How to use exercise files.

For additional information on downloading and using exercise files, watch our instructional video or read the instructions in the FAQ.

This course includes free exercise files, so you can practice while you watch the course. To access all the exercise files in our library, become a Premium Member.

Join now "Already a member? Log in

Are you sure you want to mark all the videos in this course as unwatched?

This will not affect your course history, your reports, or your certificates of completion for this course.


Mark all as unwatched Cancel

Congratulations

You have completed Building Flash Games with Starling.

Return to your organization's learning portal to continue training, or close this page.


OK
Become a member to add this course to a playlist

Join today and get unlimited access to the entire library of video courses—and create as many playlists as you like.

Get started

Already a member?

Become a member to like this course.

Join today and get unlimited access to the entire library of video courses.

Get started

Already a member?

Exercise files

Learn by watching, listening, and doing! Exercise files are the same files the author uses in the course, so you can download them and follow along. Exercise files are available with all Premium memberships. Learn more

Get started

Already a Premium member?

Exercise files video

How to use exercise files.

Ask a question

Thanks for contacting us.
You’ll hear from our Customer Service team within 24 hours.

Please enter the text shown below:

The classic layout automatically defaults to the latest Flash Player.

To choose a different player, hold the cursor over your name at the top right of any lynda.com page and choose Site preferencesfrom the dropdown menu.

Continue to classic layout Stay on new layout
Exercise files

Access exercise files from a button right under the course name.

Mark videos as unwatched

Remove icons showing you already watched videos if you want to start over.

Control your viewing experience

Make the video wide, narrow, full-screen, or pop the player out of the page into its own window.

Interactive transcripts

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.

Are you sure you want to delete this note?

No

Your file was successfully uploaded.

Thanks for signing up.

We’ll send you a confirmation email shortly.


Sign up and receive emails about lynda.com and our online training library:

Here’s our privacy policy with more details about how we handle your information.

Keep up with news, tips, and latest courses with emails from lynda.com.

Sign up and receive emails about lynda.com and our online training library:

Here’s our privacy policy with more details about how we handle your information.

   
submit Lightbox submit clicked
Terms and conditions of use

We've updated our terms and conditions (now called terms of service).Go
Review and accept our updated terms of service.