在单人游戏中添加背景无效

问题描述 投票:-2回答:1

我只想在c#单游戏主菜单中添加背景图片。我已经有了一个带有按钮和工作游戏的主菜单。只是背景不见了。这是我的代码的一部分:

public void LoadAssets()
{
    background = ScreenManager.Texture("background");
    [...] //unimportant stuff for this problem
}

 public void Draw(GameTime gameTime)
{
    SpriteBatch spriteBatch= new SpriteBatch();
    spriteBatch.Begin();
    spriteBatch.Draw(background, new Rectangle(0, 0, 800, 480), Color.White);
    spriteBatch.End();

    foreach (var button in mButtons) 
    { 
    button.Draw(ScreenManager.mSprites);
    }
}

我收到以下错误CS7036 C#,没有给出与“ SpriteBatch.SpriteBatch(GraphicsDevice)中的” graphicsDevice”的形式参数相对应的自变量。

我将图像包含在内容中。我不知道我的错误在哪里。谢谢您的帮助!

c# graphics arguments monogame
1个回答
2
投票

首先,不要在每个绘制调用中创建新的SpriteBatch实例。就像每秒60个新实例(60fps)]

相反,您可以在LoadContent()方法中创建它,并在Draw()中一直使用它方法:

protected override void LoadContent()
{
    spriteBatch = new SpriteBatch(GraphicsDevice);
    [...]
}

其次,只要您没有弄乱某些东西,GraphicsDevice就可以在您的Draw-Call中使用;]

© www.soinside.com 2019 - 2024. All rights reserved.