簡體   English   中英

如何在C#中保存布爾值

[英]How to Save a Boolean Value in C#

我正在制作類似於“ Make It Rain”或“ Cookie Clicker”的游戲。 我有一些設置,您可以在其中購買商品,以使每次單擊Cookie都可以產生更多的Cookie。 我一直在嘗試使游戲保存變量。 例如,每次點擊都會產生一個cookie。 如果您有100個cookie,則可以購買一個cookie切割器,因此每次點擊都會產生5個cookie,依此類推。 我有一個稱為cookiecutters的布爾值,這是錯誤的。 如果用戶購買了商品,則cookiecutters = true。 我想將此變量保存為true,以便當用戶關閉並重新打開表單時,每次單擊都會生成5個cookie。 這是我想出的,但是沒有用。

private void Form1_Load(object sender, EventArgs e)
{
    if (cookiecutter == true)
    {
        cookiecutter = Convert.ToBoolean(Properties.Settings.Default.cookiecutter);
        cookiecutter = Properties.Settings.Default.cookiecutter;
    }
}

private void Form1_FormClosed(object sender, FormClosedEventArgs e)
{
    if (cookiecutter== true)
    {
        Properties.Settings.Default.cookiecutter = cookiecutter = true;
    }

    Properties.Settings.Default.Save();
}

謝謝大家的幫助!

您的代碼看起來很奇怪。 嘗試進行這些更改,並確保cookiecutter設置中的初始值為false。

private void Form1_Load(object sender, EventArgs e)
{
    cookiecutter = Convert.ToBoolean(Properties.Settings.Default.cookiecutter);
}

private void Form1_FormClosed(object sender, FormClosedEventArgs e)
{
    Properties.Settings.Default.cookiecutter = cookiecutter;
    Properties.Settings.Default.Save();
}

幾乎..您可以得到價值低谷

 Properties.Settings.Default.["cookiecutter"]

代替

 Properties.Settings.Default.cookiecutter

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM