Skip to content

Enable All Screens #3

@CodeRhymesLife

Description

@CodeRhymesLife

Problem

Now that you've fixed bugs #1 and #2, you can see the title screen and navigate between options, but selecting an option doesn't work 🙀! This is madness!!! No, this is Invaders!!!

I removed the code that moves to the next screen 😆. You'll have to add it back to fix this bug 😄.

Where Is The Problem?

File: src/engine/Invaders.java
Function: run()

When this bug is fixed, selecting an option on the TitleScreen should take you to the next screen. Let's work on the fix!

Instructions

After a screen is done showing we need to get the next screen and show it. We need to keep doing this while the game has not ended.

Bugs #1 and #2 prepared us for fixing this issue. Bug #1 showed us how to create the TitleScreen using TitleScreen screen = new TitleScreen(); and how to show the TitleScreen using screen.show();. We will create and show other screens using the same technique.

Bug #2 showed us that each screen has a member in ScreenType. For example, the GameScreen has ScreenType.GameScreen, the HighScoreScreen has ScreenType.HighScoreScreen, ect...

To fix this issue we need to use the knowledge we learned in bugs #1 and #2 to create and draw the next screen.

To get the next screen's type call screen.getNextScreenType(); and store the returned value in a variable called nextScreenType. What do you think the data type for nextScreenType is? To find out, look at the type of value screen.getNextScreenType(); returns.

Now that we have the next screen's type we can detect and create the next screen. Write an if or else if statement for each of the 4 lines below:

  1. if nextScreenType equals ScreenType.TitleScreen
    create and show a new TitleScreen, similar to what you did in bug Show Title Screen #1. Then get the next screen type.
  2. else if nextScreenType equals ScreenType.GameScreen
    create and show a new GameScreen. Then get the next screen type.
    (Careful: make sure you pass the new GameScreen the gameState and levelSettings variables)
  3. else if nextScreenType equals ScreenType.ScoreScreen
    create and show a new ScoreScreen. Then get the next screen type.
    (Careful: make sure you pass the new ScoreScreen the gameState variable)
  4. else if nextScreenType equals ScreenType.HighScoreScreen
    create and show a new HighScoreScreen. Then get the next screen type.

At this point you should be able to run your code and select an option on the TitleScreen. For example, selecting the High Scores option on the TitleScreen should move you on to the HighScoresScreen. Give it a try!

You may have noticed that the game stops after the second screen. For example, pressing space on the the HighScoresScreen does not take you back to the TitleScreen like it should 😱!

It's ok, that's what was supposed to happen at this point. We're almost done, but we're not done yet. We have one more thing to do.

We need to keep drawing screens until nextScreenType equals ScreenType.EndGame. Let's use a while loop! I'll let you figure this one out on your own, with this hint: don't forget to update nextScreenType after you show each screen.

Your final code should look something like this:

while( nextScreenType does not equal ScreenType.EndGame) {

     if( nextScreenType equals ScreenType.TitleScreen) {
          ...
     }
     else if( nextScreenType equals ScreenType.GameScreen) {
          ...
     }
     and so on...
}

After you have fixed this bug:

  1. Commit and push your changes to GitHub
  2. For groups, have each group member sync, run and test the latest code to make sure everything is working properly.
  3. Move on to Enable Player Movement #4!

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions