【发布时间】:2016-01-17 13:32:25
【问题描述】:
类如下所示:
Game
has-a Player
Player
has-a SpriteInstance
SpriteInstance
has-a List<SpriteInstance>
每个渲染步骤,Game 在 Player 上调用 Draw()。 Player 在 SpriteInstance 上调用 draw()。 draw() 看起来像这样:
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);
}
}
我的预期输出是能够在屏幕上看到 SpriteInstance,和来自 SpriteInstance 的每个子项的每个纹理,但我的实际输出只是 SpriteInstance 而没有子项。我尝试过使用调试器,但它表明肯定调用了孩子的 draw() 函数。而且由于spriteBatch 是一个神奇的黑匣子,我能做到的……
传递 spriteBatch 有什么问题吗?当它应该通过引用传递时,我是否以某种方式传递它?
编辑:这里是所有的绘制函数
游戏
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);
}
播放器
public void Draw(SpriteBatch spriteBatch)
{
sprite.draw(spriteBatch);
}
edit2:
将 spriteBatch.draw() 调用带入 foreach 循环确实可以显示,但我不知道有什么区别......这也不是一个正确的修复方法。
【问题讨论】: