gpt4 book ai didi

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

转载 作者:太空宇宙 更新时间:2023-11-03 12:54:50 25 4
gpt4 key购买 nike

这是游戏的样子:

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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com