0

im sorry if im writing in weird ways. pretty new to programming, going the first year so I figured i could use some help.. im trying to get an "End screen" in my group and i have no idea really how to do it. we have three levels and after the third and last level a screen should pop up like, "Do you want to play again/Exit?" here's to my problem, how do I begin with just simply starting? I have tried myself and created a SpriteFont named "EndScreen" under Objects. now later down in "draw(GameTime)" i did this:

" // Draws the Ending screen of game

        switch (CurrentGameState)
        {
            case Gamestate.EndScreen:
                {
                    spriteBatch.Draw(Content.Load<Texture2D>("Sprites/Endscreen"), new Rectangle(0, 0, screenWidth, screenHeight), Color.White);
                    btnPlay.Draw(spriteBatch);
                    break; "

now i get the error: "unreachable code detected" I would really appreciate If you could give me some step through step.

sorry if it looks bad and some type errors, I live in sweden and new to programming and this site! I also wonder if i did the coding right and putting the code on the right places, im very insecure about programming

  • possible duplicate of [Unreachable code detected in case statement](http://stackoverflow.com/questions/2643661/unreachable-code-detected-in-case-statement) – Toon Krijthe Apr 09 '14 at 09:45

2 Answers2

3

you have an extra " after the break

Since break will jump out of the switch statement, the code path will never be able to reach this part of the code. Simply removing will fix this


void Update(GameTime g)
{
   CurrentGameState = GameState.EndScreen;
}
Sayse
  • 42,633
  • 14
  • 77
  • 146
  • I fixed that little error but the problem still remains, it still says "Unreachable code detected" – user3514378 Apr 09 '14 at 11:03
  • Then your problem is elsewhere, do you ever set CurrentGameState to Endscreen? – Sayse Apr 09 '14 at 11:06
  • sorry i didnt get you now, where do I set CurentGameState? sorry for my non-existant knowledge.. – user3514378 Apr 09 '14 at 11:11
  • wherever your end game logic is, your game should have a point where it decides its time for the end screen in there just put `CurrentGameState = GameState.Endscreen;`, it will be somehwere in your updateloop, if you don't have this, for the time being just place that line in your update loop anywhere and see if there is still unreachable code – Sayse Apr 09 '14 at 11:12
  • it still says unreachable code when i put CurrentGameState = GameState.Endscreen; , in my updateloop, have i placed it wrong from beginning, I placed it in Draw(GameTime). (the EndScreen) – user3514378 Apr 09 '14 at 11:19
  • Then your bug is elsewhere its impossible to help from current information, what line of code does it say is unreachable. please update your question – Sayse Apr 09 '14 at 11:20
  • its line 405, if that what you meant, its under case Gamestate.EndScreen: { spriteBatch.Draw(Content.Load("Sprites/Endscreen"), new Rectangle(0, 0, screenWidth, screenHeight), Color.White); btnPlay.Draw(spriteBatch); CurrentGameState = Gamestate.EndScreen; break; I then get a warning on the spriteBatch.End(); – user3514378 Apr 09 '14 at 11:22
  • you're setting current game state in the wrong place.. see my edit to answer. stick it somewhere in your update method. – Sayse Apr 09 '14 at 11:24
  • 1
    that fixed it! thanks alot for sticking with me and helping me, means alot! – user3514378 Apr 09 '14 at 11:38
0

I'm not going to give you the answer to the question you asked (@sayse already did that perfectly fine), but rather the question your code asked.

When you're drawing out your image you call the following code:

spriteBatch.Draw(Content.Load<Texture2D>("Sprites/Endscreen"), new Rectangle(0, 0, screenWidth, screenHeight), Color.White);

This means every frame that your game draws that image to the screen it is attempting to load in a new instance of the texture at "Sprites/Endscreen." While XNA is able to realize not to load it, the process of checking if it already has been loaded is somewhat slow. You may not notce it immediately, but if you get enough images on a screen drawing like that you'll notice significant "lag" or drops in framerate.

A good solution to this problem would be to make a field (class member variable) at the top of your class that this code is being called in. Make it a Texture2D and call it something relevant like endScreen. Then in LoadContent load the texture into your endScreen object. Lastly changed your call to spriteBatch.Draw() to use that Texture2D. Below I have included an example of what you might want to do.

//Fields
Texture2D endScreen;
//Load Content
endScreen = Content.Load<Texture2D>("Sprites/Endscreen");
//All Your Class Code
//Draw
spriteBatch.Draw(endScreen, new Rectangle(0, 0, screenWidth, screenHeight), Color.White);
Loothelion
  • 368
  • 1
  • 10