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);

本文标签: