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的代码
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);
}
}
答案 0 :(得分:2)
我看到了你的问题。您声明contentManager
变量,但在LoadContent
方法之前永远不会分配它。如果您在此之前加载Dirt
,则不会有有效的ContentManager
变量导致其失败。
在Salvage
类的构造函数中,添加以下行:
contentManager = Content;
这可以解决您的问题。