【发布时间】:2020-04-09 18:05:33
【问题描述】:
前言
首先,我知道在这个平台上不鼓励发布代码的图形资源。我也会发布代码,但在这种特殊情况下,我认为发布有关它的视频比发布一些任意代码更有帮助,因为游戏项目的结构确实根据他们的要求而有所不同。但是,我仍然尊重平台的规则,所以如果模组要求我根据社区规则格式化我的问题,我可以这样做,或者他们也可以简单地删除我的问题。我尊重这一点。
问题
这实际上是一个简单的问题,但由于它的简单性,它让我发疯了。我只想在加载场景时淡入,然后在单击按钮时淡出。至于我是怎么做到的,this is the video about it。
总而言之,我加载了另一个名为“Fader”的场景,其中包含黑色的ColorRect 和AnimationPlayer 以更改ColorRect 的alpha 值。
代码如下,相关部分有额外的 cmets:
using Godot;
using System;
public class TitleScreen : Control
{
private Button[] buttons;
private Control fader; // the scene that I inject
public override void _Ready() // when title screen gets ready
{
GD.Print("Preparing TitleScreen...");
InitButtons();
InitFader(); // initialize fader
FadeIn(); // do fade in animation
}
private void InitFader() // initializing fader
{
GD.Print("Initializing fader...");
var faderScene = (PackedScene)ResourceLoader.Load("res://components/Fader.tscn"); // load external fader scene
fader = (Control)faderScene.Instance(); // instantiate the scene
fader.SetSize(OS.WindowSize); // set the size of fader scene to the game window, just in case
var rect = (ColorRect)fader.GetNode("rect"); // get "rect" child from fader scene
rect.SetSize(OS.WindowSize); // set "rect" size to the game window as well, just in case
fader.Visible = false; // set the visibility to false
AddChild(fader); // add initialized fader scene as a child of title screen
}
private void InitButtons()
{
GD.Print("Initializing buttons...");
buttons = new Button[3]{
(Button)GetNode("menu_container/leftmenu_container/menu/start_button"),
(Button)GetNode("menu_container/leftmenu_container/menu/continue_button"),
(Button)GetNode("menu_container/leftmenu_container/menu/exit_button"),
};
GD.Print("Adding events to buttons...");
buttons[0].Connect("pressed", this, "_StartGame");
buttons[2].Connect("pressed", this, "_QuitGame");
}
private void FadeIn()
{
GD.Print("Fading in...");
fader.Visible = true; // set visibility of fader to true
var player = (AnimationPlayer)fader.GetNode("player"); // get animation player
player.Play("FadeIn"); // play FadeIn animation
fader.Visible = false; // set visibility of fader to false
}
private void FadeOut()
{
// similar to FadeIn
GD.Print("Fading out...");
fader.Visible = true;
var player = (AnimationPlayer)fader.GetNode("player");
player.Play("FadeOut");
fader.Visible = false;
}
public void _StartGame() // whenever I click start game button
{
FadeOut(); // fade out
GetTree().ChangeScene("res://stages/Demo01.tscn");
}
public void _QuitGame() // whenever I click quit game button
{
FadeOut(); // fade out
GetTree().Quit();
}
}
好像什么都看不到。为什么不淡入淡出?
环境
- Manjaro 19.0.2
- Mono JIT 编译器 6.4.0(如果相关)
- 戈多3.2
【问题讨论】:
-
注释掉
fader.Visible = false;声明以取得成功,它们太早隐藏了推子。 -
@HansPassant |是的,that seems to be the issue。我在
AnimationPlayer上使用animation_started和animation_finished信号解决了一半问题。谢谢。