gpt4 book ai didi

c# - Monogame 中 base.Update 和 base.Draw 的功能?

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

我正在学习来自 this website 的基本 Monogame 教程.我不明白的是为什么 Game1.cs 包含对 base.Updatebase.Draw 的调用?如果我删除这些函数调用,它似乎不会以任何方式影响我的游戏......

代码Game1.cs

        protected override void Update (GameTime gameTime)
{
// update my car class
_myCar.Update();
// what does this do? It seems I can leave it out?
base.Update (gameTime);
}

protected override void Draw (GameTime gameTime)
{
// draw my car
_spriteBatch.Begin ();
_myCar.Draw (_spriteBatch);
_spriteBatch.End ();

// what does this do?
base.Draw (gameTime);
}

最佳答案

根据source code of MonoGame由 Dylan Wilson 链接,来自基类 Game 的 Update 和 Draw 方法刚刚在 a 的每个元素上调用了 Update(...)Draw(...)过滤后的 GameComponentCollectionIGameComponent 的集合。

IGameComponent 是一个显示 XNA 和 Monogame 希望您如何管理代码的界面。它带有其他接口(interface),例如 IUpdateableIDrawable。另外两个类实现这些接口(interface)并提供逻辑:GameComponentDrawableGameComponent

这些接口(interface)和类背后的想法是,游戏的所有组件都有类似的更新/绘制周期。它标准化了方法的名称和参数(Initialize、LoadContent、Update、Draw 等),并提供了自定义组件的框架。根据您的类是否需要更新、绘制或不需要这些,您可以继承这些类。

这是 GameComponentCollection 运行的地方:您可以将您的组件添加到 Game.Components 并且基类 Game 将通过调用 Update() 和 Draw 自动处理更新周期() 无需您的干预。您还可以使用 GameComponentDrawableGameComponent 提供的属性设置组件之间的调用顺序。

无论如何,似乎有些人(包括我)和许多教程并没有在他们的代码中使用这种可能性。您可以自己从自定义类中自由调用 Initialize、LoadContent、Update 和 Draw。在这种情况下,您的程序似乎不需要调用 base.Update(...)base.Draw(...)

关于c# - Monogame 中 base.Update 和 base.Draw 的功能?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30139877/

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