【问题标题】:I am making a text based game in unity (5.2.3) I am stuck at this:我正在统一制作基于文本的游戏(5.2.3)我被困在这个:
【发布时间】:2016-03-19 21:31:44
【问题描述】:

这就是游戏的样子:

玩家必须按照上段的正确顺序点击下半部分的单词。

例如,上段以“This”开头,用户必须找到并触摸下半部的“This”。他尝试了 3 次才能做到正确。如果他点击了错误的单词,他就会失去生命。

在我的代码中,我设法通过检查它们之间的空格和点来分隔第一个字符串中的单词,并使用 StringBuilder 添加单个字母,然后将其传输到另一个字符串存储整个单词。

//Proper Working Code

public class Queue2Test : MonoBehaviour {

    private string paraString = "This is not the end of the line. This is only the beginning. It cannot get much worse.";
    private StringBuilder buffer;
    private string finalString;
    private int counter;

    void Start () {
        buffer = new StringBuilder ();
        finalString = null;
        counter = 0;
        dothis ();
    }

    void dothis () {

        for (counter = 0; counter < paraString.Length; counter++) {
            buffer = buffer.Append(paraString[counter]);
            if (paraString[counter] == ' ' || paraString[counter] == '.') {
                //buffer.Remove(counter,1);
                finalString = buffer.ToString();
                buffer.Length = 0;
                print (finalString);
            }
        }
    }
} 

哪个有效。上面代码的输出是所有的字都一一打印出来的。现在我想做的是,以这样的方式改变它,使每个单词只有在我点击鼠标时才会打印出来。为此,我这样做了:

// This code needs editing: 

public class Queue3Test : MonoBehaviour {

    private string paraString = "This is not the end of the line. This is only the beginning. It cannot get much worse.";
    private StringBuilder buffer;
    private string finalString;
    private int counter;
    private bool flag;
    private int tempvalue;

    void Start () {
        buffer = new StringBuilder ();
        finalString = null;
        counter = 0;
        flag = false;
        tempvalue = 0;
    }

    void Update () {
        if (flag) {
            dothis ();
        }
    }

    void OnMouseDown () {
        flag = true;
        //print (flag);
    }

    void dothis () {
        for (counter = tempvalue; counter < paraString.Length; counter++) {
            buffer = buffer.Append(paraString[counter]);
            if (flag) {
                if (paraString[counter] == ' ' || paraString[counter] == '.') {
                    //buffer.Remove(counter,1);
                    finalString = buffer.ToString();
                    buffer.Length = 0;
                    print (finalString);
                    tempvalue = counter;
                    flag = false;
                    continue;
                }
                //break;
            }
        }
    }
}

这段代码的输出是这样的:

This //First Click
is not the end of the line. This is only the beginning. It cannot get much worse. //Second Click

此代码在第一次单击后打印第一行,在第二次单击时它一次打印剩余的行! 我希望每个单词一个接一个地单独打印鼠标点击

我点击段落右侧的一个小碰撞器来调用鼠标点击功能。

我是使用 C# 编程的新手,我正在为我的大学项目制作这款游戏​​。这方面的任何帮助都会很有用,或者只是将我指向正确的方向。我肯定会在游戏的学分上提到回答者的名字!!!

谢谢。 :)

【问题讨论】:

  • 尝试将break;而不是continue;放在代码的最后一个循环中
  • @ClaudiuGeorgiu 我这样做了,但它不起作用。它使我脱离了循环,并且无法从循环外再次恢复循环。 :(

标签: c# arrays string unity3d queue


【解决方案1】:

首先,关于您的第一个问题。 你所要做的就是打电话

words = paraString
    .Split(new[] { ' ', '.' }, StringSplitOptions.RemoveEmptyEntries)
    .ToList();

这将返回一个单词数组,忽略所有空格和空字符。从这里开始,您可以执行以下操作:

void OnMouseDown() {
    var wordToShow = words.Skip(currentWord).FirstOrDefault();
    if (!String.IsNullOrEmpty(wordToShow)) {
        currentWord++;
        Debug.Log(wordToShow);
    } else {
        Debug.Log("Nothing left...");
    }
}

请务必将单词列表存储为您的行为脚本的私有字段,如下所示:

private IList<string> words;

您可以通过在 Start() 或 Awake() 函数中拆分输入字符串来初始化它。我会使用开始。

【讨论】:

    【解决方案2】:

    首先,您应该避免使用这种命名变量和函数的方式(标志、dothis)。 其次,您选择了不合适的解决方案来解决任务。它可以更简单地解决。

    using UnityEngine;
    using System;
    
    public class Queue3Test : MonoBehaviour {
    
        private string paraString = "This is not the end of the line. This is only the beginning. It cannot get much worse.";
        private string[] words;
        private int counter = 0;
    
        private void Start()
        {
            InitializeParaString();
        }
    
        private void OnMouseDown()
        {
            ShowNextWord();
        }
    
        private void InitializeParaString()
        {
            counter = 0;
            words = paraString.Split(new[] { '.', ' ' }, StringSplitOptions.RemoveEmptyEntries);
        }
    
        private void ShowNextWord()
        {
            if (counter < words.Length)
            {
                print(words[counter]);
                counter++;
            }
        }
    }
    

    祝你好运!

    【讨论】:

      猜你喜欢
      • 2015-09-10
      • 1970-01-01
      • 2021-10-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-02-23
      • 1970-01-01
      相关资源
      最近更新 更多