Я создаю базовую игру в инфраструктуре XNA/Monogame. Я использую DrawableGameComponent так, чтобы его функции Draw() и Update() вызывались автоматически.Почему DrawableGameComponent необходимо вызвать base.Draw()?
До сих пор так хорошо, но я не понимаю, что такое base.Draw() относится к экземпляру DrawableGameComponent и зачем вам это нужно. В моем примере я могу даже оставить его, и игра все еще работает нормально.
фрагмент кода: (вопросы в комментариях)
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);
}
Да, но в данном случае речь идет о самом верхнем классе, который наследуется непосредственно от DrawableGameComponent. В этом компоненте функция рисования пуста, поэтому нет необходимости называть ее с помощью base.Draw(). --- и это на самом деле ответ на мой собственный вопрос. base.Draw() можно безопасно удалить. – Kokodoko