不在主游戏类中时无法加载内容
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我已经看到你的问题了。您声明了
contentManager
变量,但直到使用LoadContent
方法时才分配它。如果您在此之前加载Dirt
,则不会有有效的ContentManager
变量,从而导致其失败。在
Salvage
类的构造函数中,添加以下行:contentManager = Content;
这应该可以解决您的问题。
I have seen your problem. You declare the
contentManager
variable, but you never assign it until theLoadContent
method. If you are loadingDirt
before this, you won't have a validContentManager
variable causing it to fail.In your constructor of your
Salvage
class, add the following line:contentManager = Content;
This should resolve your issue.