[英]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.