2014-11-21 41 views
0

我正在尝试使一些按钮更改变量,所以我不必每次编写新代码都要新建一个按钮,而是创建了一个可以为我实现的功能。参数不按预期工作

“void buttons”方法有4个参数,但“numberOF”参数不起作用。当我点击按钮时,它应该改变“numberOf”和“price”的争论,但由于某种原因,它永远不会改变“numberOf”。

希望有人在这里,可以帮助我。我知道有很多我没有用过的东西。每次当我第一次尝试时,我都会用长码做按钮,所以还有一些无用的代码仍然需要清理。

using UnityEngine; 
using System.Collections; 

public class Gui : MonoBehaviour 
{ 

public int one; 

    //Money 
    public int money; 

    //Iventory 
    public int stone; 
    public int jord; 
    public int frø; 
    public int korn; 
    public int brød; 

    //Item price 
    public int stonePrice; 
    public int jordPrice; 
    public int frøPrice; 
    public int kornPrice; 
    public int brødPrice; 


    //Item inventory text and button text 
    private string moneyText; 
    private string stoneText; 
    private string jordText; 
    private string frøText; 
    private string kornText; 
    private string brødText; 

    private string stoneButton; 
    private string jordButton; 
    private string frøButton; 
    private string kornButton; 
    private string brødButton; 

    //Screen heith/width = 0 
    private int screenWidth; 
    private int screenHeight; 


    // Runs one time 
    void Start() 
    { 

     //Set variables to screen heith/width to 0 
     screenWidth = Screen.width - Screen.width; 
     screenHeight = Screen.height - Screen.height; 

     //Money reset 
     money = 10000; 

     //inventory reset 
     stone = 0; 
     jord = 0; 
     frø = 0; 
     korn = 0; 
     brød = 0; 

     //item price reset 
     stonePrice = 1; 
     jordPrice = 3; 
     frøPrice = 5; 
     kornPrice = 7; 
     brødPrice = 9; 

     //Set item text 
     moneyText = "money: " + money + " kr."; 
     stoneText = " stone " + stone; 
     jordText = " jord " + jord; 
     frøText = " frø " + frø; 
     kornText = " korn " + korn; 
     brødText = " brød " + brød; 

     //set button text 
     stoneButton = "Stone " + stonePrice + " kr."; 
     jordButton = "Jord " + jordPrice + " kr."; 
     frøButton = "Frø " + frøPrice + " kr."; 
     kornButton = "Korn " + kornPrice + " kr."; 
     brødButton = "Brød " + brødPrice + " kr."; 

    } 


    void Update() 
    { 
     stone = stone; 
     jord = jord; 
     frø = frø; 
     korn = korn; 
     brød = brød; 

     moneyText = moneyText; 
     stoneText = stoneText; 
     jordText = jordText; 
     frøText = frøText; 
     kornText = kornText; 
     brødText = brødText; 

     //Check item text changes 
     moneyText = "money: " + money + " kr."; 
     stoneText = " stone " + stone; 
     jordText = " jord " + jord; 
     frøText = " frø " + frø; 
     kornText = " korn " + korn; 
     brødText = " brød " + brød; 

     //Check button text changes 
     stoneButton = "Stone " +stonePrice + " kr."; 
     jordButton = "Jord " + jordPrice + " kr."; 
     frøButton = "Frø " + frøPrice + " kr."; 
     kornButton = "Korn " + kornPrice + " kr."; 
     brødButton = "Brød " + brødPrice + " kr."; 

    } 

    void OnGUI() 
    { 

     buttons(150, stoneButton, stone, stonePrice); 

     if (GUI.Button (new Rect (Screen.width - 100, Screen.height - 20, 100, 20), "End Turn")) 
     { 
      newRound(); 
     } 


     //Iventory 
     GUI.TextArea (new Rect (screenWidth + 1, screenHeight + 2, Screen.width, 20),moneyText + "  Inventory: " + stoneText + jordText + frøText + kornText + brødText); 


    } 

    void make_buttons(int position_heigth, string buttonText, int numberOF, int price) 
    { 
     GUI.TextArea (new Rect (screenWidth + 2, screenHeight + position_heigth + 80, 80, 20), buttonText); 
     if (GUI.Button (new Rect (screenWidth + 80, screenHeight + position_heigth + 80, 40, 20), "Buy")) 
     { 
      if (money > price) 
      { 
       numberOF = 1 + numberOF; 
       money = money - price; 
      } 

      else if (money == price) 
      { 
       numberOF = 1 + numberOF; 
       money = money - price; 
      } 
     } 
     if (GUI.Button (new Rect (screenWidth + 120, screenHeight + position_heigth + 80, 40, 20), "Sell")) 
     { 
      if (numberOF > 0) 
      { 
       numberOF = numberOF - 1; 
       money = money + price; 
      } 
     } 
    } 

    void newRound() 
    { 
     stonePrice = stonePrice * 2; 
     jordPrice = jordPrice * 2; 
     frøPrice = frøPrice * 2; 
     kornPrice = kornPrice * 2; 
     brødPrice = brødPrice * 2; 
    } 

} 
+0

我在这里没有看到问题。只是一个广泛的求助请求。你被卡住了什么? – techvice 2014-11-21 16:20:56

+0

“我知道有很多东西我不使用” - 请保持良好并编辑代码以显示相关部分...可能需要阅读“通过引用传递”,但很难说如果这是真正的问题,你已经发布的代码墙。 – 2014-11-21 16:23:29

+0

**女朋友错误405:参数不能正常工作**:D – LearnCocos2D 2014-11-21 16:33:18

回答

1

我相信你是说该函数不会修改“numberOf”的值。 这并不完全正确,但是你正在做的是修改一个现在的本地值。原因是你传入一个int,它不是通过引用传递,而是通过值传递。

你也许可以通过修改numberOf到ref numberOf来解决这个问题,但是如果可能的话,我会建议你只是返回numberOf。理解发生的事情更简单明了。

我还建议更好的变量和方法名称buttons,因为它不是全部清楚什么行动buttons应该执行。

关于在C#this article中使用ref参数的更多信息非常好。

+0

正确,我也看不到任何理由分配Update()函数中的变量。如果因为它们包含完全相同的代码段,“购买”if/else也可以缩短为单个。 – 2014-11-21 16:50:14

+0

是一个实验,看看它是否会有所帮助,有点新的C#:) – 2014-11-21 17:15:13