![](/img/trans.png)
[英]An unhandled exception of type 'System.ArgumentNullException' occurred in mscorlib.dll Additional information: Path cannot be null
[英]An unhandled exception of type 'System.ArgumentNullException' occurred in Microsoft.Xna.Framework.Graphics.dll
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。
通過一些試驗和錯誤,我發現正是這部分代碼引起了異常:該行中的tile [tileID]:spriteBatch.Draw(tiles [tileID],tilePos,Color.White);但是我可以不知道該怎么辦。 我簽出了MonoGame.Framework.dll中發生的'System.ArgumentNullException'類型的未處理異常 ,但是由於tile []和tileID都被初始化,所以它沒有幫助我。 謝謝您對這個問題的任何幫助。 :P
我的代碼:
您正在調用的方法不是以Null作為參數,如果您沒有行號,並且該行的方法我們真的不知道哪個方法可能導致異常
spriteBatch.Begin(SpriteSortMode.BackToFront, BlendState.AlphaBlend, null, null, null, null, cam.get_transformation(device));
看起來可能是由此方法引起的,您可以在此處放置一個斷點並檢查值並在此處發布定義嗎?
這是因為在創建createMap
對象map
,紋理(草,石頭,污垢)為空,因此會使用空項目填充地圖對象中的陣列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
}
因此,這將是解決當前問題的快速簡便的方法。 但是,在沒有大量構造函數的情況下加載包含大量靜態對象和類的項目通常會在一段時間后迅速導致難以管理的代碼庫。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.