The classes look like this:
Game
has-a Player
Player
has-a SpriteInstance
SpriteInstance
has-a List<SpriteInstance>
Each render step, Game
calls Draw()
on Player
. Player
calls draw()
on SpriteInstance
. And draw()
looks like this:
public void draw(SpriteBatch spriteBatch, Vector2 offset = default(Vector2), float rotation = 0f)
{
spriteBatch.Draw(texture, offset + pos, null, Color.White, rot+rotation, sprite.origin, 1f, SpriteEffects.None, z);
foreach (var child in children)
{
child.draw(spriteBatch, offset + pos, rotation + rot);
}
}
My expected output is being able to see both the SpriteInstance on the screen, and each texture from each child of SpriteInstance, but my actual output is just the SpriteInstance and none of the children. I have tried using a debugger, but it shows that the child's draw() function is definitely called. And since spriteBatch
is a magical black box that's as far as I can go...
Is there something wrong about passing spriteBatch around? Am I passing it by value somehow when it should be passed by reference?
edit: here are all the draw functions
Game
protected override void Draw(GameTime gameTime)
{
GraphicsDevice.Clear(Color.CornflowerBlue);
// TODO: Add your drawing code here
spriteBatch.Begin();// sortMode: SpriteSortMode.FrontToBack);
player.Draw(spriteBatch);
spriteBatch.End();
base.Draw(gameTime);
}
Player
public void Draw(SpriteBatch spriteBatch)
{
sprite.draw(spriteBatch);
}
edit2:
Bringing the spriteBatch.draw() calls out into the foreach loop does make it display, but I don't know what the difference is... it is also not a proper fix.