【问题标题】:Unity3D Code repeats too muchUnity3D代码重复太多
【发布时间】:2015-12-30 22:09:27
【问题描述】:

我试图让如果你有足够的金币然后你需要岩石,如果你有足够的岩石那么你需要足够的金币,但如果你有两个那么你可以“升级”。但如果你两者兼得,那么它又回到了你需要黄金的状态。

    void Update()
    {
        if(enoughgold == true & enoughrocks == true)
        {
            Upgrade.text = "Upgrade to 2014!";
        }

        if(sellrocks.gold > 9999)
        {
            enoughgold = true;
        }
        else
        {
            enoughgold = false;
        }

        if(click.rock > 2999)
        {
            enoughrocks = true;
        }
        else
        {
            enoughrocks = false;
        }

        if(enoughgold == true)
        {
            Upgrade.text = "You need 3,000 Rocks!";
        }
        else
        {
            Upgrade.text = "You need 10,000 Gold!";
        }

        if (enoughrocks == true)
        {
            Upgrade.text = "You need 10,000 Gold!";
        }
        else
        {
            Upgrade.text = "You need 3,000 Rocks!";
        }
    }

【问题讨论】:

    标签: c# unity3d unityscript


    【解决方案1】:

    这样的事情怎么样?你先看看用户是否有足够的金子和石头,然后进行检查。
    我已将if (enoughgold == true) 简化为if (enoughgold),因为== true 是多余的。

    void Update()
    {
        enoughgold = sellrocks.gold > 9999;
        enoughrocks = click.rock > 2999;
    
        if (enoughgold && enoughrocks)
            Upgrade.text = "Upgrade to 2014!";
        else if (enoughgold && !enoughrocks)
            Upgrade.text = "You need 3,000 Rocks!";
        else if (!enoughgold && enoughrocks)
            Upgrade.text = "You need 10,000 Gold!";
        else if (!enoughgold && !enoughrocks)
            Upgrade.text = "You need 10,000 Gold and 3,000 Rocks!";
    }
    

    您还可以创建一个枚举来处理所有 4 种可能性:如果用户只有足够的石头,如果用户只有足够的金币,如果用户有足够的两种,如果用户没有足够的任何一种。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-01-25
      • 2015-03-28
      • 1970-01-01
      • 2023-01-03
      • 1970-01-01
      • 1970-01-01
      • 2010-09-22
      • 2023-04-09
      相关资源
      最近更新 更多