不在主游戏类中时无法加载内容

发布于 2024-11-11 23:53:25 字数 4544 浏览 3 评论 0原文

public class Dirt : Tile
{
    Vector2 position;
    Texture2D texture;

    public Dirt(Game game, Vector2 Position)
        : base(game)
    {
        type = "dirt";
        textureName = "Textures/Dirt";

        position = Position;
    }

    public override void Initialize()
    {

        base.Initialize();
    }

    protected override void LoadContent()
    {
        texture = Salvage.contentManager.Load<Texture2D>("Textures/Dirt"); // This doesnt work

        base.LoadContent();
    }

    public override void Update(GameTime gameTime)
    {
        if (InputHandler.KeyDown(Keys.W))
        {
            position.Y -= 1;
        }
        else if (InputHandler.KeyDown(Keys.S))
        {
            position.Y += 1;
        }

        if (InputHandler.KeyDown(Keys.D))
        {
            position.X += 1;
        }
        else if (InputHandler.KeyDown(Keys.A))
        {
            position.X -= 1;
        }

        Camera.DesiredPosition = position;

        base.Update(gameTime);
    }

    public override void Draw(GameTime gameTime)
    {
        Salvage.spriteBatch.Begin(SpriteSortMode.BackToFront, BlendState.AlphaBlend, null, null, null, null, Camera.Transformation);
        Salvage.spriteBatch.Draw(texture, position, Color.White);
        Salvage.spriteBatch.End();


        base.Draw(gameTime);
    }
}

如果我在 Salvage 类(主游戏类)中加载它,加载似乎工作正常

编辑:

我得到的错误是:

Salvage.spriteBatch.Draw(texture,position,Color.White); 此方法不接受此参数为 null。 参数名称:texture

这是 Salvage 类的代码:

public class Salvage : Microsoft.Xna.Framework.Game
{

    public static GraphicsDeviceManager graphics;
    public static SpriteBatch spriteBatch;
    public static ContentManager contentManager;
    public static SpriteFont spriteFont;
    public GameStateManager stateManager;
    public Rectangle ScreenRectangle = new Rectangle(0, 0, 1024, 768);

    public Salvage()
    {
        graphics = new GraphicsDeviceManager(this);
        graphics.PreferredBackBufferHeight = ScreenRectangle.Height;
        graphics.PreferredBackBufferWidth = ScreenRectangle.Width;

        Content.RootDirectory = "Content";
        contentManager = Content;

    }

    protected override void Initialize()
    {

        base.Initialize();
    }

    protected override void LoadContent()
    {
        spriteBatch = new SpriteBatch(GraphicsDevice);
        spriteFont = Content.Load<SpriteFont>("Fonts/DefaultSpriteFont"); // This works fine

        stateManager = new GameStateManager(this);
        GameplayState gameplayState = new GameplayState(this, stateManager);

        Components.Add(new InputHandler(this));
        Components.Add(new Camera(this, ScreenRectangle));
        Components.Add(stateManager);

        stateManager.ChangeState(gameplayState);

    }


    protected override void UnloadContent()
    {

    }

    protected override void Update(GameTime gameTime)
    {
        if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed || InputHandler.KeyReleased(Keys.Escape))
            this.Exit();

        base.Update(gameTime);
    }

    protected override void Draw(GameTime gameTime)
    {
        GraphicsDevice.Clear(Color.CornflowerBlue);

        base.Draw(gameTime);
    }
}

以及 Tile Edit 的代码

public abstract class Tile : Microsoft.Xna.Framework.DrawableGameComponent
{
    public string textureName;
    public string type;

    public Tile(Game game)
        : base(game)
    {

    }

    public override void Initialize()
    {

        base.Initialize();
    }

    protected override void LoadContent()
    {

        base.LoadContent();
    }

    public override void Update(GameTime gameTime)
    {

        base.Update(gameTime);
    }

    public override void Draw(GameTime gameTime)
    {

        base.Draw(gameTime);
    }
}

这是 GamePlayState 类

public class GameplayState : GameState
{

    public GameplayState(Game game, GameStateManager manager)
        : base(game, manager)
    {
        childGameComponents.Add(new Dirt(game, new Vector2(0, 0)));
    }

    public override void Initialize()
    {

        base.Initialize();
    }

    protected override void LoadContent()
    {
        base.LoadContent();
    }

    public override void Update(GameTime gameTime)
    {

        base.Update(gameTime);
    }

    public override void Draw(GameTime gameTime)
    {

        base.Draw(gameTime);
    }
}
public class Dirt : Tile
{
    Vector2 position;
    Texture2D texture;

    public Dirt(Game game, Vector2 Position)
        : base(game)
    {
        type = "dirt";
        textureName = "Textures/Dirt";

        position = Position;
    }

    public override void Initialize()
    {

        base.Initialize();
    }

    protected override void LoadContent()
    {
        texture = Salvage.contentManager.Load<Texture2D>("Textures/Dirt"); // This doesnt work

        base.LoadContent();
    }

    public override void Update(GameTime gameTime)
    {
        if (InputHandler.KeyDown(Keys.W))
        {
            position.Y -= 1;
        }
        else if (InputHandler.KeyDown(Keys.S))
        {
            position.Y += 1;
        }

        if (InputHandler.KeyDown(Keys.D))
        {
            position.X += 1;
        }
        else if (InputHandler.KeyDown(Keys.A))
        {
            position.X -= 1;
        }

        Camera.DesiredPosition = position;

        base.Update(gameTime);
    }

    public override void Draw(GameTime gameTime)
    {
        Salvage.spriteBatch.Begin(SpriteSortMode.BackToFront, BlendState.AlphaBlend, null, null, null, null, Camera.Transformation);
        Salvage.spriteBatch.Draw(texture, position, Color.White);
        Salvage.spriteBatch.End();


        base.Draw(gameTime);
    }
}

The loading seems to work fine if i load it in the Salvage class(Main game class)

Edit:

The error im getting is:

Salvage.spriteBatch.Draw(texture, position, Color.White);
This method does not accept null for this parameter.
Parameter name: texture

Here is the code for Salvage class:

public class Salvage : Microsoft.Xna.Framework.Game
{

    public static GraphicsDeviceManager graphics;
    public static SpriteBatch spriteBatch;
    public static ContentManager contentManager;
    public static SpriteFont spriteFont;
    public GameStateManager stateManager;
    public Rectangle ScreenRectangle = new Rectangle(0, 0, 1024, 768);

    public Salvage()
    {
        graphics = new GraphicsDeviceManager(this);
        graphics.PreferredBackBufferHeight = ScreenRectangle.Height;
        graphics.PreferredBackBufferWidth = ScreenRectangle.Width;

        Content.RootDirectory = "Content";
        contentManager = Content;

    }

    protected override void Initialize()
    {

        base.Initialize();
    }

    protected override void LoadContent()
    {
        spriteBatch = new SpriteBatch(GraphicsDevice);
        spriteFont = Content.Load<SpriteFont>("Fonts/DefaultSpriteFont"); // This works fine

        stateManager = new GameStateManager(this);
        GameplayState gameplayState = new GameplayState(this, stateManager);

        Components.Add(new InputHandler(this));
        Components.Add(new Camera(this, ScreenRectangle));
        Components.Add(stateManager);

        stateManager.ChangeState(gameplayState);

    }


    protected override void UnloadContent()
    {

    }

    protected override void Update(GameTime gameTime)
    {
        if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed || InputHandler.KeyReleased(Keys.Escape))
            this.Exit();

        base.Update(gameTime);
    }

    protected override void Draw(GameTime gameTime)
    {
        GraphicsDevice.Clear(Color.CornflowerBlue);

        base.Draw(gameTime);
    }
}

And the code for Tile

public abstract class Tile : Microsoft.Xna.Framework.DrawableGameComponent
{
    public string textureName;
    public string type;

    public Tile(Game game)
        : base(game)
    {

    }

    public override void Initialize()
    {

        base.Initialize();
    }

    protected override void LoadContent()
    {

        base.LoadContent();
    }

    public override void Update(GameTime gameTime)
    {

        base.Update(gameTime);
    }

    public override void Draw(GameTime gameTime)
    {

        base.Draw(gameTime);
    }
}

Edit:

Here is the GamePlayState class

public class GameplayState : GameState
{

    public GameplayState(Game game, GameStateManager manager)
        : base(game, manager)
    {
        childGameComponents.Add(new Dirt(game, new Vector2(0, 0)));
    }

    public override void Initialize()
    {

        base.Initialize();
    }

    protected override void LoadContent()
    {
        base.LoadContent();
    }

    public override void Update(GameTime gameTime)
    {

        base.Update(gameTime);
    }

    public override void Draw(GameTime gameTime)
    {

        base.Draw(gameTime);
    }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

月棠 2024-11-18 23:53:25

我已经看到你的问题了。您声明了 contentManager 变量,但直到使用 LoadContent 方法时才分配它。如果您在此之前加载 Dirt,则不会有有效的 ContentManager 变量,从而导致其失败。

Salvage 类的构造函数中,添加以下行:

contentManager = Content;

这应该可以解决您的问题。

I have seen your problem. You declare the contentManager variable, but you never assign it until the LoadContent method. If you are loading Dirt before this, you won't have a valid ContentManager variable causing it to fail.

In your constructor of your Salvage class, add the following line:

contentManager = Content;

This should resolve your issue.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文