代码之家  ›  专栏  ›  技术社区  ›  Глеб Буткевич

如何分步使用函数

  •  -1
  • Глеб Буткевич  · 技术社区  · 5 年前

    很抱歉这个奇怪的标题,我只是不知道怎么命名这个问题。

    所以我有这样的功能 say() .

    void say(string printedText) {
        gameText.text = printedText;
    }
    

    我要用几次。像这样的:

    say("test text 1");
    say("test text 2");
    say("test text 3");
    ...
    

    我需要通过单击空格键来更改文本。我当然要用这样的东西:

    if(Input.GetKeyDown(KeyCode.Space)) {
       ...
    }
    

    但我不明白如何一步一步地显示文本。例如,如果我单击空格键一次,我就会看到“测试文本1”。下一次点击应该显示“测试文本2”等。

    我怎么能意识到呢?提前谢谢。

    4 回复  |  直到 5 年前
        1
  •  3
  •   derHugo    5 年前

    根据您的需要,您可以在 List<string> 或者甚至 Queue<string> 并且做

    列表示例

    // Add your texts in the editor or by calling texts.Add(someNewString)
    public List<string> texts = new List<string>();
    
    private int index = 0;
    
    if(Input.GetKeyDown(KeyCode.Space)) 
    {
        // have a safety check if the counter is still within 
        // valid index values
        if(texts.Count > index) say(texts[index]);
        // increase index by 1
        index++;
    }
    

    数组示例

    基本上和 列表<字符串> 但是你不能“即时”添加或删除元素(至少不是那么简单)

    public string[] texts;
    
    private int index = 0;
    
    if(Input.GetKeyDown(KeyCode.Space)) 
    {
        // have a safety check if the counter is still within 
        // valid index values
        if(texts.Length > index) say(texts[index]);
        // increase index by 1
        index++;
    }
    

    队列示例

    public Queue<string> texts = new Queue<string>();
    

    要在队列末尾添加新文本,请执行以下操作

    texts.Enqueue(someNewString);
    

    然后

    if(Input.GetKeyDown(KeyCode.Space)) 
    {
        // retrieves the first entry in the queue and at the same time
        // removes it from the queue
        if(texts.Count > 0) say(texts.Dequeue());
    }
    

    简易计数器

    如果它真的只是一个不同的int值,那么yes只需使用一个字段

    private int index;
    
    if(Input.GetKeyDown(KeyCode.Space)) 
    {
        // uses string interpolation to replace {0} by the value of index
        say($"test text {0}", index);
        // increase index by one
        index++;
    }
    
        2
  •  2
  •   Denis Schaf    5 年前

    定义一个类字段,如下所示:

    int count = 0;
    

    现在每次空间被击中:

    if(Input.GetKeyDown(KeyCode.Space)) {
        say("test text " + count);
        count = count + 1;
    }
    
        3
  •  0
  •   icrescenti    5 年前

    此代码:

    if(Input.GetKeyDown(KeyCode.Space)) {
       ...
    }
    

    仅适用于Unity,在Visual Studio中,您必须为任何要执行此操作的对象创建事件,例如,如果每次按空格键时都要调用void,则必须执行此操作,这很简单:(下图)

    在propeties窗口中,按下bolt图标,双击要创建的事件(示例):textchanged、locationchnaged、mousemove等…

    我会用 按下 文本框对象

    image

    现在在您的代码中,应该生成这个void

    image

    在这个空白中,我编写了代码,这就是它的外观:

    (在空隙前输入int n=1)

    private void textBox1_KeyDown(object sender, KeyEventArgs e)
            {
                if(e.KeyCode == Keys.Space)
                {
                    //int n = 1; must be defined
                    textBox1.Text = "test text " + n; 
                    n++;
                }
            }
    

    现在,每次按下或保持按下空格键时,文本框将填充“测试文本”,并且每次的值将增加1。