简体   繁体   English

如何在C#中保存布尔值

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

I am making a game similar to "Make it Rain" or "Cookie Clicker." 我正在制作类似于“ Make It Rain”或“ Cookie Clicker”的游戏。 I have settings where you can purchase items to make each click of the cookie produce more cookies. 我有一些设置,您可以在其中购买商品,以使每次单击Cookie都可以产生更多的Cookie。 I have been trying to make the game save the variable. 我一直在尝试使游戏保存变量。 For example, each click produces a cookie. 例如,每次点击都会产生一个cookie。 If you have 100 cookies, you can buy a cookie cutter, so each click produces 5 cookies, and so on. 如果您有100个cookie,则可以购买一个cookie切割器,因此每次点击都会产生5个cookie,依此类推。 I have booleans called cookiecutters, which are false. 我有一个称为cookiecutters的布尔值,这是错误的。 If the user buys the item, cookiecutters = true. 如果用户购买了商品,则cookiecutters = true。 I would like to save this variable to true so when the user closes and reopens the form, each click produces 5 cookies. 我想将此变量保存为true,以便当用户关闭并重新打开表单时,每次单击都会生成5个cookie。 This is what I have come up with, yet it doesn't work. 这是我想出的,但是没有用。

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();
}

Thank you all for the help in advance! 谢谢大家的帮助!

Your code looks weird. 您的代码看起来很奇怪。 Try to make these changes and make sure your initial value in the settings for cookiecutter is false. 尝试进行这些更改,并确保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();
}

Almost.. you can get the value trough 几乎..您可以得到价值低谷

 Properties.Settings.Default.["cookiecutter"]

instead of 代替

 Properties.Settings.Default.cookiecutter

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

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