我正在尝试使一些按钮更改变量,所以我不必每次编写新代码都要新建一个按钮,而是创建了一个可以为我实现的功能。参数不按预期工作
“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;
}
}
我在这里没有看到问题。只是一个广泛的求助请求。你被卡住了什么? – techvice 2014-11-21 16:20:56
“我知道有很多东西我不使用” - 请保持良好并编辑代码以显示相关部分...可能需要阅读“通过引用传递”,但很难说如果这是真正的问题,你已经发布的代码墙。 – 2014-11-21 16:23:29
**女朋友错误405:参数不能正常工作**:D – LearnCocos2D 2014-11-21 16:33:18