子场景可以“幻灯片”吗?在运行时进入父场景?

发布于 2025-01-16 20:10:53 字数 490 浏览 1 评论 0原文

是否可以在运行时将场景(输入Control)“滑动”到场景中间。

我有一个“父”场景,中间有一个空白区域。我想将一系列场景“滑动”到父场景的中央空白区域。

我想做这样的事情,但是使用动画:

var qi = load("res://Child1.tscn").instance()
qi.get_node("/root/Child1")
$VBoxContainer/MiddleArea.add_child(qi)

然后,使用不同或相同的数据在相同或另一种类型的场景中滑动:

var qi = load("res://Child1.tscn").instance()
qi.get_node("/root/Child1")
$VBoxContainer/MiddleArea.add_child(qi)

该序列由数据库中的数据驱动,因此在设计时不可预测场景可能需要出现的顺序的时间。

Is it possible to "slide" a scene (type Control) into the middle of a scene at runtime.

I have a "parent" scene, with a blank area in the middle. I would like to "slide" a sequence of scenes into the central blank area of the parent scene.

I want to do something like this, but with animation:

var qi = load("res://Child1.tscn").instance()
qi.get_node("/root/Child1")
$VBoxContainer/MiddleArea.add_child(qi)

Then later on, slide in the same, or another type of scene, with different or the same data:

var qi = load("res://Child1.tscn").instance()
qi.get_node("/root/Child1")
$VBoxContainer/MiddleArea.add_child(qi)

The sequence is driven by data in a database, so its not predictable at design time what sequence of scenes might need to appear.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

巴黎盛开的樱花 2025-01-23 20:10:53

如果我理解正确的话,你想要的可以用 Tween 来完成。

因此,首先将 Tween 添加到场景树中。我们可以将它保存在一个变量中,如下所示:

onready var tween := $Tween

然后我们可以使用它来制作一个简单的动画:

$VBoxContainer/MiddleArea.add_child(qi)

var duration_seconds := 2.0
tween.interpolate_property(
    qi,
    "rect_position",
    Vector2(-qi.rect_size.x, qi.rect_position.y),
    qi.rect_position,
    duration_seconds,
    Tween.TRANS_CUBIC,
    Tween.EASE_OUT
)
tween.start()

此代码将为场景的位置设置动画。起始位置 Vector2(-qi.rect_size.x, qi.rect_position.y) 位于屏幕左侧。最终位置是场景在动画之前的位置。

如果您要在这一行循环中添加多个以等待动画结束,然后再执行下一个:

yield(tween, "tween_all_completed")

如果您要将场景添加到设置场景位置的容器中,您可能需要像这样添加它this:

qi.modulate = Color.transparent
$Container.add_child(qi)
yield(get_tree(), "idle_frame")
qi.self_modulate = Color.white

这里 qi.modulate = Color.transparent 将使场景不可见。然后我们将其添加到容器中,并使用 yield(get_tree(), "idle_frame") 等待一帧,在此期间容器设置场景的位置。最后我们使用 qi.self_modulate = Color.white 来恢复可见性。 我们必须使场景不可见,这样它就不会出现一帧然后消失然后进行动画处理。我们不使用 visible 因为容器可能会跳过定位场景。

If I understand correctly, what you want can be done with a Tween.

So, first off all add Tween to the scene tree. We can hold it in a variable like this:

onready var tween := $Tween

Then we can use it to make a simple animation:

$VBoxContainer/MiddleArea.add_child(qi)

var duration_seconds := 2.0
tween.interpolate_property(
    qi,
    "rect_position",
    Vector2(-qi.rect_size.x, qi.rect_position.y),
    qi.rect_position,
    duration_seconds,
    Tween.TRANS_CUBIC,
    Tween.EASE_OUT
)
tween.start()

This code will animate the position of the scene. The starting position Vector2(-qi.rect_size.x, qi.rect_position.y) is just outside of the screen to the left. And the final position is wherever the scene is positioned before the animation.

If you are adding multiple in a loop at this line to await the end of the animation before doing the next one:

yield(tween, "tween_all_completed")

And if you are adding the scene to a container that is setting the position of the scene, you might want to add it like this:

qi.modulate = Color.transparent
$Container.add_child(qi)
yield(get_tree(), "idle_frame")
qi.self_modulate = Color.white

Here qi.modulate = Color.transparent will make the scene invisible. Then we add it to the container, and wait for one frame with yield(get_tree(), "idle_frame"), during which the container set the position of the scene. And finally we use qi.self_modulate = Color.white to restore the visibility. We have to make the scene invisible so it does not appear for one frame just to disappear and then be animated. And we don't use visible because the container might skip positioning the scene.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文