0

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.

Sky
  • 220
  • 2
  • 14

1 Answers1

0

Try this and let me know if it works.

public void draw(SpriteBatch spriteBatch, Vector2 offset = default(Vector2), float rotation = 0f)
{
spriteBatch.Begin(); //Starts the drawing loop

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);
}
spriteBatch.End();  // This is for ending the drawing loop.
}

If that doesn't work I would like to see your main class, and a copy of the output you are getting when you run the application. Please also list any errors that occur.

RayF
  • 11
  • 3
  • I already call Begin and End. There is no console output, because *something* shows up on the screen (the parent SpriteInstance) but not the children SpriteInstances. Posted the code tho. – Sky Jan 17 '16 at 05:33
  • Can you show where your loading the content? Also in the draw method what parameter are you sending the null to? – RayF Jan 17 '16 at 05:42
  • I figured it out, and it was a silly mistake--the z-buffer range is from 0 to 1 only, and I was trying to pass z=2 for the children. – Sky Jan 17 '16 at 05:44
  • And the few times it isn't it feels even worse! :P – Sky Jan 17 '16 at 05:52