c# - 我正在统一制作一个基于文本的游戏 (5.2.3) 我被困在这个 :

标签 c# arrays string unity3d queue

这是游戏的样子:

Prototype1

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

例如,上段以“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# 进行编程还很陌生,我正在为我的大学项目制作这款游戏​​。这方面的任何帮助都是有用的,或者只是为我指明了正确的方向。我肯定会在游戏致谢名单上提到回答者的名字!!!

谢谢。 :)

最佳答案

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

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)字段,如下所示:

private IList<string> words;

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

关于c# - 我正在统一制作一个基于文本的游戏 (5.2.3) 我被困在这个 :,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34266411/

相关文章:

c# - 在 C# 中重用或处理自定义字体的有效方法

c# - C++ 中 cv::Rectangle 的问题

C# - Int 操作莫名其妙地返回负值

javascript - 如何根据 Javascript 数组中的日期计算预订价格

c - 接收字符串大小并让您在 C 中逐个字母地创建它的程序

python - 将等于 json 字典的字典转换

c# - 跟踪游戏对象变换 - 为什么使用 FindGameObjectWithTag 不起作用?

html - 显示以逗号分隔且末尾没有逗号的值列表

javascript - 将给定的字符串拆分为数组

python - 将字符串打印到文本文件