【问题标题】:Sprite won't render at all?Sprite 根本不会渲染?
【发布时间】:2016-01-17 13:32:25
【问题描述】:

类如下所示:

Game
has-a Player

Player
has-a SpriteInstance

SpriteInstance
has-a List<SpriteInstance>

每个渲染步骤,GamePlayer 上调用 Draw()PlayerSpriteInstance 上调用 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 循环确实可以显示,但我不知道有什么区别......这也不是一个正确的修复方法。

【问题讨论】:

    标签: c# xna monogame


    【解决方案1】:

    试试这个,让我知道它是否有效。

    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.
    }
    

    如果这不起作用,我想查看您的主类,以及您在运行应用程序时获得的输出副本。还请列出发生的任何错误。

    【讨论】:

    • 我已经调用了 Begin 和 End。没有控制台输出,因为 something 显示在屏幕上(父 SpriteInstance)而不是子 SpriteInstances。把代码贴出来了。
    • 您能显示您加载内容的位置吗?同样在 draw 方法中,您将 null 发送到哪个参数?
    • 我想通了,这是一个愚蠢的错误——z-buffer 的范围只是从 0 到 1,我试图为孩子传递 z=2。
    • 这几次不是感觉更糟! :P
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-02-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多