C# texture2d中的NullReference异常
我已尝试在C# texture2d中的NullReference异常,c#,visual-studio,xna,xna-4.0,C#,Visual Studio,Xna,Xna 4.0,我已尝试在PlayerTank类构造函数上加载纹理。但是我收到了NullReferenceException namespace BattleCityv1._5.Tanks { class PlayerTank : Tank { public PlayerTank(int life, Point position) { texture = Content.Load<Texture2D>(@"images/Play
PlayerTank
类构造函数上加载纹理。但是我收到了NullReferenceException
namespace BattleCityv1._5.Tanks
{
class PlayerTank : Tank
{
public PlayerTank(int life, Point position)
{
texture = Content.Load<Texture2D>(@"images/PlayerTankSpriteSheet");
Life = life;
Rectangle = new Rectangle(position.X, position.Y, 32, 32);
Source = new Rectangle(0, 0, Rectangle.Width, Rectangle.Height);
type = "player1";
Color = Color.White;
}
}
}
namespace BattleCityv1.\u 5.5坦克
{
职业玩家银行:坦克
{
公共玩家库(int-life,Point-position)
{
纹理=Content.Load(@“图像/PlayerTankSpriteSheet”);
生命=生命;
矩形=新矩形(位置.X,位置.Y,32,32);
Source=新矩形(0,0,矩形.宽度,矩形.高度);
type=“player1”;
颜色=颜色。白色;
}
}
}
变量内容是ContentManager吗?这些变量是否已声明?您的类“Tank”没有扩展Microsoft.Xna.Framework.Game,是吗
内容可能为空。但是内容应该只在扩展了Microsoft.Xna.Framework.Game的类中(可能会命名为BattleCityv1或其他)
我建议您从扩展Microsoft.Xna.Framework.Game的类中获取纹理。制作一个静态字典,一次加载所有纹理,并通过访问该字典获得这些纹理。这可能是最简单的解决方案。纹理对象从未真正创建过
texture = ContentManager.Load<Texture2D>(@"images/PlayerTankSpriteSheet");
不是资产名称。它是一个文件路径。您需要单击解决方案资源管理器中“内容”下的精灵,然后找到资产名称。这是作为ContentManager的参数键入的内容。在哪一行加载@user5767028?
@"images/PlayerTankSpriteSheet"