简体   繁体   English

C#中的关闭函数

[英]closing functions in C#

I am doing a game in XNA with C#. 我正在使用C#在XNA中进行游戏。 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: 如果我开始游戏后迷路了,我将重新初始化所有列表/功能,然后重新启动游戏。如果我第二次按下“ P”键(暂停),然后选择“进入主菜单”选项,则会出现一个大错误(NotSupportedException)。请在此处查看:

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. 为了解决这个问题,我想如果Count == 0只能执行MainMenu,而在选择播放后我想关闭它。 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. 通过使用else if块,可以确保在每次调用更新期间仅执行一个选项。 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; 另外,您也可以通过简单地调用return;解决这种情况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;
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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