繁体   English   中英

C#中的关闭函数

[英]closing functions in C#

我正在使用C#在XNA中进行游戏。 我做了一个主菜单,然后有“新游戏”选项。 如果我开始游戏后迷路了,我将重新初始化所有列表/功能,然后重新启动游戏。如果我第二次按下“ 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.
    }
{

为了解决这个问题,我想如果Count == 0只能执行MainMenu,而在选择播放后我想关闭它。 与其他功能相同,我想在使用后将其关闭。 我该怎么做? 有可能这样做吗? 还有另一种方法吗? (对不起,如果您听不懂我的英语,请告诉我)。

我相信您要问的是“一旦选择了计数选项,我将如何无法继续执行更新功能”。 通过使用else if块,可以确保在每次调用更新期间仅执行一个选项。 尝试这个:

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.
    }
}

另外,您也可以通过简单地调用return;解决这种情况return; 在函数中您不想继续的任何时候。 像这样:

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