类型为';System.ArgumentNullException';出现在Microsoft.Xna.Fr

本文关键字:Microsoft Xna Fr ArgumentNullException System 类型 | 更新日期: 2023-09-27 18:07:53

Game.cs

using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
namespace Toast_Engine
{
    public class Game : Microsoft.Xna.Framework.Game
    {
        GraphicsDeviceManager graphics;
        GraphicsDevice device;
        Texture2D background;
        Texture2D player;
        public static Texture2D stone;
        public static Texture2D dirt;
        public static Texture2D grass;
        SpriteBatch spriteBatch;
        public Rectangle playerCollision;
        public Rectangle backgroundRectangle;
        createMap map = new createMap();
        Camera cam = new Camera();
        public Vector2 playerPos;
        int playerHealth = 100;
        int playerHeight = 30;
        int playerWidth = 30;
        public Game()
        {
            graphics = new GraphicsDeviceManager(this);
            Content.RootDirectory = "Content";
        }
        protected override void Initialize()
        {
            base.Initialize();
         
            cam.cameraInit();
            playerPos = new Vector2(0, 0);
           
        }
        protected override void LoadContent()
        {
            spriteBatch = new SpriteBatch(GraphicsDevice);
            device = graphics.GraphicsDevice;
            background = Content.Load<Texture2D>("plain");
            player = Content.Load<Texture2D>("Player_Test");
            grass = Content.Load<Texture2D>("grass");
            dirt = Content.Load<Texture2D>("dirt");
            stone = Content.Load<Texture2D>("stone");
        }
        protected override void UnloadContent()
        {
        }
        protected override void Update(GameTime gameTime)
        {
            //UPDATING CODE
            //THIS TEXT IS HERE TO MAKE THE CODE EASY TO SPOT AMONGST OTHER CODE
            //1234567890QWERTYUIOP
            if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed)
                this.Exit();
            keyCheck();
            positionPlayerCollision();
            positionCamera();
            positionBackground();
            
            base.Update(gameTime);
        }
        private void positionBackground()
        {
            int screenWidth = GraphicsAdapter.DefaultAdapter.CurrentDisplayMode.Width;
            int screenHeight = GraphicsAdapter.DefaultAdapter.CurrentDisplayMode.Height;
            backgroundRectangle = new Rectangle(0, 0, screenWidth, screenHeight);
        }
        public void positionCamera()
        {
            cam.Pos = playerPos;
        }
        private void positionPlayerCollision()
        {
            playerCollision = new Rectangle((int)playerPos.X, (int)playerPos.Y, playerWidth, playerHeight);
        }
        private void drawPlayer()
        {
            spriteBatch.Draw(player, playerCollision, Color.White);
        }
        private void drawBackground()
        {
            spriteBatch.Draw(background, backgroundRectangle, Color.White);
        }
        public void keyCheck()
        {
            KeyboardState keysPressed = Keyboard.GetState();
            if(keysPressed.IsKeyDown(Keys.W))
            {
                playerPos.Y--;
            }
            if (keysPressed.IsKeyDown(Keys.A))
            {
                playerPos.X--;
            }
            if (keysPressed.IsKeyDown(Keys.S))
            {
                playerPos.Y++;
            }
            if (keysPressed.IsKeyDown(Keys.D))
            {
                playerPos.X++;
            }
        }
        protected override void Draw(GameTime gameTime)
        {
            GraphicsDevice.Clear(Color.CornflowerBlue);
            // TODO: Add your drawing code here
            //DRAWING CODE
            //THIS TEXT IS HERE TO MAKE THE CODE EASY TO SPOT AMONGST OTHER CODE
            //1234567890QWERTYUIOP
            spriteBatch.Begin(SpriteSortMode.BackToFront, BlendState.AlphaBlend, null, null, null, null, cam.get_transformation(device));
            drawPlayer();
            map.renderMap(spriteBatch);
            drawBackground();
            //Console.WriteLine(cam.Pos);
            spriteBatch.End();
            base.Draw(gameTime);
        }
    }
}

createMap.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
namespace Toast_Engine
{
    class createMap
    {
        
        public int amountOfTiles = 4;
        public int mapWidth = 5;
        public int mapHeight = 10;
        public int tileSize = 32;
        private Vector2 test = new Vector2(5, 5);
        public int[,] tileMap = new int[,]
        {
        {1,1,2,0,0,2,0,0,1,3},
        {3,0,0,3,3,0,0,0,3,0},
        {2,2,2,2,2,2,2,2,2,2},
        {2,1,1,1,1,1,1,1,1,2},
        {1,1,1,1,1,1,1,1,1,1}
        };
        public Texture2D[] tiles = new Texture2D[] 
        {
           Game.grass,Game.grass,Game.dirt,Game.stone
        };
        
        public void renderMap(SpriteBatch spriteBatch)
        {
            Console.WriteLine("Thing actually running");
            for (int x = 0; x < mapWidth; x++)
            {
                for (int y = 0; y < mapHeight; y++)
                {
                    Console.WriteLine("X = " + x + ", Y = " + y);
                    Vector2 tilePos = new Vector2((x+1)*tileSize, (y+1)*tileSize);
                    int tileID = tileMap[x, y];
                    if(tileID != 0)
                    {
                        Console.WriteLine("Tile ID of " + x + "," + y + " is " + tileID+" , Texture is "+tiles[tileID]);
                        spriteBatch.Draw (tiles[tileID], tilePos, Color.White);
                    }
                }
            }
        }
    }
}

我正试图在Xna中创建一个简单的自上而下的瓷砖游戏,但每当我运行该程序时,我都会收到以下错误:

Microsoft.Xna.Framework.Graphics.dll 中出现类型为"System.ArgumentNullException"的未处理异常

附加信息:此方法不接受此参数的null。

通过反复尝试,我发现正是这段代码导致了异常:行中的tiles[tileID]:spriteBatch.Draw(tile[ID],tilePos,Color.White(;,但我不知道该怎么办。我签出了一个类型为';System.ArgumentNullException';发生在MonoGame.Framework.dll中,但它对我没有帮助,因为tile[]和tileID都已初始化。如有任何帮助,我们将不胜感激,谢谢P

我的代码:

类型为';System.ArgumentNullException';出现在Microsoft.Xna.Fr

这是因为当创建createMap对象map时,纹理(草、石头、泥土(为null,因此它会用null项填充贴图对象中的数组tiles。稍后,当您绘制它们时,第一个参数(texture2d(不允许为null,因此它会抛出错误。

如果您尝试运行代码,它会停在spriteBatch.Draw行并变为黄色。将鼠标悬停在tiles[tileID]上,然后在其下方单击+号。您将看到3个空纹理。

但是,如果你只是简单地声明map,而不尝试"新建"它,然后在LoadContent()方法中加载纹理后"新建",它会很好地工作

class game
{
  public static Texture2d grass;
  public createMap map;

protected override void LoadContent()
        {
            spriteBatch = new SpriteBatch(GraphicsDevice);
            device = graphics.GraphicsDevice;
            background = Content.Load<Texture2D>("plain");
            player = Content.Load<Texture2D>("Player_Test");
            grass = Content.Load<Texture2D>("grass");
            dirt = Content.Load<Texture2D>("dirt");
            stone = Content.Load<Texture2D>("stone");
//then create the map
                map = new createMap();
            }
     //rest of class
}

因此,这将是克服眼前问题的快捷方法。然而,在没有构造函数的情况下加载带有大量静态对象和类的项目通常会很快导致难以管理代码库。

您正在调用一个不将Null作为参数的方法,如果您没有行号和该行的方法,我们真的不知道哪个方法可能导致异常

spriteBatch.Begin(SpriteSortMode.BackToFront, BlendState.AlphaBlend, null, null, null, null, cam.get_transformation(device));

看起来它可能是由这个方法引起的,你能在那里放一个断点,检查值并在这里发布定义吗?