个性化阅读
专注于IT技术分析

在Godot中进行线编辑

在本教程中, 我们看到了如何以程序员的身份向自己显示文本, 以及如何将其显示在输出中以及如何向播放器显示文本。这不仅可能是我们游戏中的关键组成部分, 而且也是将来我们可能制作的许多游戏的基本原理。我们现在必须使用Godot引擎:

有行编辑和文本编辑。现在它们之间的区别是我们希望玩家能够按回车键转到下一行或返回输入文本, 而行编辑允许玩家输入单行文本, 例如, 如果我们要输入名称或用一个字写一个文本, 我们希望人们在该文本中填写很多信息, 因此, 如果我们要制作角色扮演游戏, 并且希望玩家编写自己的日记, 我们会使用文本编辑, 我不想要做到这一点。

Godot线编辑

LineEdit区域:

Godot线编辑

然后, 我们可以将其拖到所需的任何位置。并根据我们的用途使其更大。

Godot线编辑

输出:这是我们可以写入的框。

Godot线编辑

这是一种文本编辑器, 如下所示。

Godot线编辑

然后, 我们必须添加一个HBoxContainer。 HBoxContainer(水平框容器)包含一列中的内容。

Godot线编辑

但是在这里我们正在创建VBoxContainer。我们将LineEdit和DisplayText放入VBoxContainer中。因此, 我们将拖动LineEdit和displayText, 如下所示:

Godot线编辑

我们可以在屏幕截图中看到以下内容:

Godot线编辑

然后我们可以像这样将displayText拖动到LineEdit的上方。

Godot线编辑

让我们从头到尾更改VBoxContainer(垂直框容器)的对齐方式。

单击VBoxContainer, 然后从开始到结束更改对齐方式。

Godot线编辑

输出如下:

Godot线编辑

我们可以根据需要更改锚点, 也可以根据我们的要求更改边距和所有内容。

Godot线编辑

之后, 我们必须选择LineEdit并更改其字体数据, 大小, 间距和所有我们需要的内容。

Godot线编辑

我们还可以根据用途更改或调整字体大小和轮廓。

Godot线编辑

如果运行此命令, 则会给我们以下错误:

Godot线编辑

它不起作用, 因为我们的脚本指向了某个东西。如下所示, 在$($)符号后将DisplayText拖动到编码区域中:

Godot线编辑

然后我们必须删除逗号, 如下面的屏幕截图所示:

extends Control
func _ready():
	var prompts = ["MANGO", "Papaya", "Glorious", "pleasant"]
	var story= "There are many trees of %s and %s in the %s garden. And a %s morning"
	print(story%prompts)
	$VBoxContainer/DisplayText.text= story%prompts

代码如下:

Godot线编辑

然后将输出:

Godot线编辑

如果我们在下面的文本框中输入任何内容, 则不会发生任何事情, 如下所示:

Godot线编辑

然后, 我们将名称LineText重命名为PlayerText。

Godot线编辑

选择它之后, 在PlayerText的右侧有所有这些选项:在Node选项中, 有许多函数可以使用。

Godot线编辑

右键单击PlayerText, 然后单击链接上的”打开文档”。

Godot线编辑

然后它将在我们的屏幕中打开:

Godot线编辑

向下滚动后:

Godot线编辑

然后单击text_entered()从右侧进行连接。

Godot线编辑

在这里我们无法连接它, 因此我们将其关闭。

我们将在这里选择嘴唇:

Godot线编辑
Godot线编辑

在代码部分, 我们与Lonny Lips相连;

Godot线编辑

代码如下:

extends Control
func _ready():
	var prompts = ["MANGO", "Papaya", "Glorious", "pleasant"]
	var story= "There are many trees of %s and %s in the %s garden. And a %s morning"
	print(story%prompts)
	$VBoxContainer/DisplayText.text= story%prompts
func _on_PlayerText_text_entered(new_text):
	$VBoxContainer/DisplayText.text= new_text

屏幕截图:

Godot线编辑

输出:在这里我们可以看到它现在正在工作。我们在TextBox中编写的所有内容都会显示在屏幕上。

Godot线编辑

在下面的代码中, 我们添加了一个PlayerText.clear()函数, 用于在输出屏幕中看到在文本框中编写的内容, 并将其清除到Textbox中, 以便我们可以在TextBox中编写其他内容。此代码有助于在编写后清除文本框。

extends Control
func _ready():
	var prompts = ["MANGO", "Papaya", "Glorious", "pleasant"]
	var story= "There are many trees of %s and %s in the %s garden. And a %s morning"
	print(story%prompts)
	$VBoxContainer/DisplayText.text= story%prompts
func _on_PlayerText_text_entered(new_text):
	$VBoxContainer/DisplayText.text= new_text
	$VBoxContainer/PlayerText.clear()

屏幕截图:

Godot线编辑
Godot线编辑

我们还可以使用以下代码使它更简洁并易于理解:

extends Control
func _ready():
	var prompts = ["MANGO", "Papaya", "Glorious", "pleasant"]
	var story= "There are many trees of %s and %s in the %s garden. And a %s morning"
	print(story%prompts)
	$VBoxContainer/DisplayText.text= story%prompts
func _on_PlayerText_text_entered(new_text):
	update_DisplayText(new_text)


func update_DisplayText(new_text):
	$VBoxContainer/DisplayText.text= new_text
	$VBoxContainer/PlayerText.clear()

输出如下:

Godot线编辑

在下一个教程中, 我们将了解Button。我们如何同时使用TextBox和Buttons。


赞(0)
未经允许不得转载:srcmini » 在Godot中进行线编辑

评论 抢沙发

评论前必须登录!