admin管理员组文章数量:1130349
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using System.Collections.Generic;
namespace spaceWar
{
public class Game1 : Game
{
private GraphicsDeviceManager _graphics;
private SpriteBatch _spriteBatch;
private Spaceship player;
private List<Bullet> bullets;
public Game1()
{
_graphics = new GraphicsDeviceManager(this)
{
PreferredBackBufferWidth = 300,
PreferredBackBufferHeight = 600
};
Content.RootDirectory = "Content";
IsMouseVisible = true;
}
protected override void Initialize()
{
player = new Spaceship(new Vector2(150, 500));
bullets = new List<Bullet>();
base.Initialize();
}
protected override void LoadContent()
{
_spriteBatch = new SpriteBatch(GraphicsDevice);
player.LoadContent(Content);
foreach (var bullet in bullets)
bullet.LoadContent(Content);
}
protected override void Update(GameTime gameTime)
{
KeyboardState keyboardState = Keyboard.GetState();
if (keyboardState.IsKeyDown(Keys.Escape))
Exit();
player.Update(gameTime, keyboardState, bullets);
foreach (var bullet in bullets)
bullet.Update(gameTime);
bullets.RemoveAll(b => b.Position.Y < 0);
base.Update(gameTime);
}
protected override void Draw(GameTime gameTime)
{
GraphicsDevice.Clear(Color.Black);
_spriteBatch.Begin();
player.Draw(_spriteBatch);
foreach (var bullet in bullets)
bullet.Draw(_spriteBatch);
_spriteBatch.End();
base.Draw(gameTime);
}
}
public class Bullet
{
public Vector2 Position;
private Texture2D btexture;
private int speed = 10;
public Bullet(Vector2 startPosition)
{
Position = startPosition;
}
public void LoadContent(Microsoft.Xna.Framework.Content.ContentManager content)
{
if(btexture == null)
btexture = content.Load<Texture2D>("bullet");
}
public void Update(GameTime gameTime)
{
Position.Y -= speed;
}
public void Draw(SpriteBatch spriteBatch)
{
spriteBatch.Draw(btexture, Position, Color.White);
}
}
}
I don't understand why i got the exception on line
spriteBatch.Draw(btexture, Position, Color.White);
, can someone let me know why my bullets arent bulleting.
I tried LoadTexture my bullets elsewhere, rebuilding bullet.png.
'Position' comes from spaceship class but from my tests this issue has nothing to do with 'Position' so i excluded that part from here.
ToString() output of exception is "An exception of type 'MonoGame.Framework.dll' occurred on System.ArgumentNullException."
Ok so i solved the problem(kinda), i gave up on the "bullet.png", used an TextureManager instead
public static class TextureManager
{
public static Texture2D Pixel { get; private set; }
public static void Initialize(GraphicsDevice graphicsDevice)
{
Pixel = new Texture2D(graphicsDevice, 1, 1);
Pixel.SetData(new[] { Color.White });
}
}
it looks like this, and the changes in bullet class
public class Bullet
{
public Vector2 Position;
private int speed = 10;
private Rectangle rectangle;
private Color color = Color.White;
public Bullet(Vector2 startPosition)
{
Position = startPosition;
rectangle = new Rectangle((int)startPosition.X, (int)startPosition.Y, 3, 7);
}
public void Update(GameTime gameTime)
{
Position.Y -= speed;
rectangle.Y = (int)Position.Y;
}
public void Draw(SpriteBatch spriteBatch)
{
spriteBatch.Draw(TextureManager.Pixel, rectangle, Color.White);
}
}
Basically if you can't load your texture you can use a colored rectangle instead for basic things
And you have to add this under "Game1" classes "LoadContent()"
TextureManager.Initialize(GraphicsDevice);
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using System.Collections.Generic;
namespace spaceWar
{
public class Game1 : Game
{
private GraphicsDeviceManager _graphics;
private SpriteBatch _spriteBatch;
private Spaceship player;
private List<Bullet> bullets;
public Game1()
{
_graphics = new GraphicsDeviceManager(this)
{
PreferredBackBufferWidth = 300,
PreferredBackBufferHeight = 600
};
Content.RootDirectory = "Content";
IsMouseVisible = true;
}
protected override void Initialize()
{
player = new Spaceship(new Vector2(150, 500));
bullets = new List<Bullet>();
base.Initialize();
}
protected override void LoadContent()
{
_spriteBatch = new SpriteBatch(GraphicsDevice);
player.LoadContent(Content);
foreach (var bullet in bullets)
bullet.LoadContent(Content);
}
protected override void Update(GameTime gameTime)
{
KeyboardState keyboardState = Keyboard.GetState();
if (keyboardState.IsKeyDown(Keys.Escape))
Exit();
player.Update(gameTime, keyboardState, bullets);
foreach (var bullet in bullets)
bullet.Update(gameTime);
bullets.RemoveAll(b => b.Position.Y < 0);
base.Update(gameTime);
}
protected override void Draw(GameTime gameTime)
{
GraphicsDevice.Clear(Color.Black);
_spriteBatch.Begin();
player.Draw(_spriteBatch);
foreach (var bullet in bullets)
bullet.Draw(_spriteBatch);
_spriteBatch.End();
base.Draw(gameTime);
}
}
public class Bullet
{
public Vector2 Position;
private Texture2D btexture;
private int speed = 10;
public Bullet(Vector2 startPosition)
{
Position = startPosition;
}
public void LoadContent(Microsoft.Xna.Framework.Content.ContentManager content)
{
if(btexture == null)
btexture = content.Load<Texture2D>("bullet");
}
public void Update(GameTime gameTime)
{
Position.Y -= speed;
}
public void Draw(SpriteBatch spriteBatch)
{
spriteBatch.Draw(btexture, Position, Color.White);
}
}
}
I don't understand why i got the exception on line
spriteBatch.Draw(btexture, Position, Color.White);
, can someone let me know why my bullets arent bulleting.
I tried LoadTexture my bullets elsewhere, rebuilding bullet.png.
'Position' comes from spaceship class but from my tests this issue has nothing to do with 'Position' so i excluded that part from here.
ToString() output of exception is "An exception of type 'MonoGame.Framework.dll' occurred on System.ArgumentNullException."
Ok so i solved the problem(kinda), i gave up on the "bullet.png", used an TextureManager instead
public static class TextureManager
{
public static Texture2D Pixel { get; private set; }
public static void Initialize(GraphicsDevice graphicsDevice)
{
Pixel = new Texture2D(graphicsDevice, 1, 1);
Pixel.SetData(new[] { Color.White });
}
}
it looks like this, and the changes in bullet class
public class Bullet
{
public Vector2 Position;
private int speed = 10;
private Rectangle rectangle;
private Color color = Color.White;
public Bullet(Vector2 startPosition)
{
Position = startPosition;
rectangle = new Rectangle((int)startPosition.X, (int)startPosition.Y, 3, 7);
}
public void Update(GameTime gameTime)
{
Position.Y -= speed;
rectangle.Y = (int)Position.Y;
}
public void Draw(SpriteBatch spriteBatch)
{
spriteBatch.Draw(TextureManager.Pixel, rectangle, Color.White);
}
}
Basically if you can't load your texture you can use a colored rectangle instead for basic things
And you have to add this under "Game1" classes "LoadContent()"
TextureManager.Initialize(GraphicsDevice);
本文标签:
版权声明:本文标题:c# - How to resolve `ArgumentNullException` exception thrown from SpriteBatch.Draw(btexture, Position, Color) when btexture was 内容由热心网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:https://it.en369.cn/questions/1736128886a1385662.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。


发表评论