【发布时间】:2015-03-11 21:53:22
【问题描述】:
我正在 XNA / Monogame 框架中创建一个基本游戏。我使用 DrawableGameComponent 以便自动调用它的 Draw() 和 Update() 函数。
到目前为止,一切都很好,但我不明白 base.Draw() 在 DrawableGameComponent 实例中指的是什么,以及为什么需要它。在我的示例中,我什至可以省略它,游戏仍然可以正常运行。
代码摘录:(cmets中的问题)
Game1.cs
protected override void Initialize()
{
Car car3 = new Car(this, new Vector2(450, 100), Color.GreenYellow);
base.Initialize();
}
protected override void Draw(GameTime gameTime)
{
GraphicsDevice.Clear(Color.CornflowerBlue);
SpriteBatch.Begin();
// this calls Game.Draw - and this loops through all the game components and draws them
base.Draw(gameTime);
SpriteBatch.End();
}
还有 Car.cs
public override void Draw(GameTime gameTime)
{
// this draws the car after Car.Draw() gets called by the Game class
Game1.SpriteBatch.Draw(Texture,Position,Color);
// but why do we call base.Draw here? The car is already drawn.
// and the DrawableGameComponent.Draw method is empty, so we dont really need to call that?
// what's more, if I comment this code out, the game still works.
base.Draw(gameTime);
}
【问题讨论】: