简体   繁体   中英

closing functions in C#

I am doing a game in XNA with C#. I made a Main menu and there is the option "new game". If I start the game and I lose I reinitialize all the list/functions and I can restart the game.If I press "P" key (pause) the second time I play and I choose "go to main menu" option there comes out a big error (NotSupportedException).take a look here:

int count=0;
bool paus=false;
public void mainMenu()
{
   //...
}

public void updateGame(GameTime gameTime)
{
   //...
}

public void pause()
{
   //...
}
public void ending()
{
   //...
}

//in the Update method:
protected override Update(GameTime gameTime)
}
    if(count==0)
    {
        mainMenu();//if I press "New Game" I put count=1
    }

    if(count==1)
    {    
        if(pause==false)
            updateGame(gameTime);//if I die count=2, if I press P pause=true
    }
    if(pause==true && count==1)
    {
        pause();
    }

    if(count==2)
    {
        ending();//If I press "retry" I reinitialize all and put count=1;
                 //If I press "main menu" I go to main menu and this functions
                 //If I retry to play(retry button) , I press "P" key and then I choose to go to "main menu" there comes out an error (NotSupportedException) If I do it the first time it functions.
    }
{

To solve that problem I though that if Count==0 to execute only MainMenu and after choosing to play I want to close it. Same with others functions, I want to close them after being used. How can I do it? Is it possible to do that? Is there another way to do that? (sorry for my english if you don't understand something tell me).

I believe what you are asking, is "how can I not continue executing the update function, once a count option has been selected". By instead using else if blocks, you can ensure that only one of your options is going to be executed during each call to update. Try this:

protected override Update(GameTime gameTime)
{
    if(count==0)
    {
        mainMenu();//if I press "New Game" I put count=1
    }
    else if(count==1)
    {    
        if(pause==false)
            updateGame(gameTime);//if I die count=2, if I press P pause=true
    }
    else if(pause==true && count==1)
    {
        pause();
    }
    else if(count==2)
    {
        ending();//If I press "retry" I reinitialize all and put count=1;
                 //If I press "main menu" I go to main menu and this functions
                 //If I retry to play(retry button) , I press "P" key and then I choose to     go to "main menu" there comes out an error (NotSupportedException) If I do it the first     time it functions.
    }
}

Alternatively, you could also fix this situation by simply calling return; at any point in the function where you do not want to continue. Like this:

protected override Update(GameTime gameTime)
{
    if(count==0)
    {
        mainMenu();//if I press "New Game" I put count=1
        return;
    }

    if(count==1)
    {    
        if(pause==false)
        {
            updateGame(gameTime);//if I die count=2, if I press P pause=true
            return;
        }
    }
    if(pause==true && count==1)
    {
        pause();
        return;
    }

    if(count==2)
    {
        ending();//If I press "retry" I reinitialize all and put count=1;
                 //If I press "main menu" I go to main menu and this functions
                 //If I retry to play(retry button) , I press "P" key and then I choose to      go to "main menu" there comes out an error (NotSupportedException) If I do it the first     time it functions.
        return;
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM