如何在XNA(VB.NET)中创建启动画面(或类似功能)

时间:2019-04-11 16:41:50

标签: vb.net visual-studio xna startup splash-screen

我在XNA上加载游戏的时间很疯狂(最多1分钟),并且在第一次绘制之前加载时的屏幕是白色的,这使人们认为这是一个错误并关闭了应用程序。我需要帮助为他们创建某种消息(例如启动屏幕),以便他们知道白屏是正在加载的屏幕,或者更好的是,将白屏交换为图片。

游戏项目是使用VB.NET在XNA中创建的。到目前为止,我找到的答案要么适用于其他事物,要么没有奏效。 我尝试在加载内容部分(由一个人建议)中添加图片的绘制,例如:

spritebach.begin
draw my picture
spritebatch.end
GraphicsDevice.Present()

但是,这什么都不做。图片甚至没有显示,直到第一次绘制为止,都会显示相同的白屏。

需要明确的是,我不希望在游戏开始后(如对其他帖子的一些回答所建议的那样)显示“按下按钮即可开始”的图片。加载后,我已经有一个介绍视频。在加载游戏时,我想要一些东西而不是白屏。

其他测试:

我尝试在loadcontent和update函数中进行绘制(等待所有资产的加载),但是除了draw函数,它没有绘制任何其他函数。另外,当我在绘图功能中放置“ Exit Sub”时,加载时间减少到几秒钟。因此,加载内容本身并不耗费时间,而是如果未提前放置退出子项,则是第一次加载draw函数。

对此将提供任何帮助!

1 个答案:

答案 0 :(得分:1)

好的。我的答案可能不完全是您想要的,因为它专门针对Monogame(XNA)。但我认为想法是相同的。以下是我如何在游戏中实现启动画面的方法。

在声明阶段:

bool isLoadded;//= false by default

在LoadContent()中:

//Load background image and spriteFont if necessary
fontBold = Content.Load<SpriteFont>("Arial_Normal_Windows"),//spriteFont
texLoadingBackground = Content.Load<Texture2D>(@"Arts\loadingBackground");
imageLoadingBackground = new ImageBackground(texLoadingBackground, Color.White, GraphicsDevice);//load the background you need to draw
//Then load everything you need after that
LoadGameContent();//
//After loading everything
isLoaded = true;

在Draw()方法中:

    if (!isLoaded)
    {
        GraphicsDevice.Clear(new Color(222, 184, 135));

        spriteBatch.Begin(SpriteSortMode.Deferred, BlendState.NonPremultiplied);

        imageLoadingBackground.Draw(spriteBatch);//only draw the background image while loading
        spriteBatch.DrawString(fontBold, "LOADING...",
            new Vector2(screenWidth / 2 - fontBold.MeasureString("LOADING...").X / 2 * scaleLarge,
            screenHeight / 2),
            Color.Black,
            0, new Vector2(0, 0), scaleLarge, SpriteEffects.None, 0);//and some text

        spriteBatch.End();
    }

    if (isLoaded)
    {        
        spriteBatch.Begin();
        //draw your main screen here
        spriteBatch.End();
    }

此外,在我的主要LoadContent()方法中,我使用:

ThreadPool.QueueUserWorkItem(state =>
            {
                LoadGameContent();
            });

调用该方法来加载我的游戏内容。

只需更改语法以匹配您的VBA程序以及Draw()方法即可。

编辑:用于计时器以计数加载时间:

声明一个变量:long loadTimeForSplashContent, loadTimeForAllContent, startTime;

LoadContent()方法的第一行:loadTimeForSplashContent = DateTime.Now.Ticks;

加载初始屏幕的内容后:loadTimeForSplashContent = (DateTime.Now.Ticks - start) / 10000;//to convert to seconds

加载所有内容后:loadTimeForAllContent = (DateTime.Now.Ticks - start) / 10000;//to convert to seconds

然后将它们打印到屏幕或Console上以查看它们的数量。您需要在DateTime.Now.Ticks中找到一种类似的VBA方法,才能看到此时的时间(抱歉,我不知道)。

希望这会有所帮助!