Код: Выделить всё
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 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();
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("bullet");
}
public void Update(GameTime gameTime)
{
Position.Y -= speed;
}
public void Draw(SpriteBatch spriteBatch)
{
spriteBatch.Draw(btexture, Position, Color.White);
}
}
}
Код: Выделить всё
spriteBatch.Draw(btexture, Position, Color.White);
Я попробовал LoadTexture свои пули в другом месте, пересобрав файл Bullet.png, перезагрузив Bullet.png и вообще не использовать Bullet.png (пытался разместить белый прямоугольник шириной/высотой 3/7).
'Position' происходит из класса космического корабля, но, судя по моим тестам, эта проблема не имеет ничего общего иметь отношение к «Позиции» поэтому я исключил эту часть отсюда.
Вывод исключения ToString(): «Исключение типа MonoGame.Framework.dll произошло в System.ArgumentNullException».
Подробнее здесь: https://stackoverflow.com/questions/792 ... tch-drawbt