XNA-创建纹理时出现空引用异常
每次按下空格键时,我都需要拍摄精灵。我很确定我已经创建了纹理并绘制了它(在我的player类中),但是每次它读取XNA-创建纹理时出现空引用异常,xna,nullreferenceexception,texture2d,Xna,Nullreferenceexception,Texture2d,每次按下空格键时,我都需要拍摄精灵。我很确定我已经创建了纹理并绘制了它(在我的player类中),但是每次它读取bulletTexture.width时,它总是返回null。我希望有人能帮助我 下面是我的代码的完整摘要: Player.cs using System; using System.Collections.Generic; using System.Linq; using System.Text; using Microsoft.Xna.Framework; using Micro
bulletTexture.width
时,它总是返回null。我希望有人能帮助我
下面是我的代码的完整摘要:
Player.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.Input;
namespace SpaceShooter_Beta.Animation.PlayerCollection
{
public class Player : SpriteManager
{
#region Field Region
public bool isAnimating = false, isDirectional = false;
private float loadTime = 0.05f, shootDelay, bulletDelay, timeElapsed;
public float speed;
public List<CommonBullet> commonBulletList;
public int bulletCap = 20;
KeyboardState kbs;
#endregion
#region Property Region
public int fps
{
set { loadTime = (1f / value); }
}
#endregion
#region Constructor Region
public Player(Texture2D texture, int frame, int animations) :
base(texture, frame, animations)
{
speed = 2f;
shootDelay = 5f;
this.AddAnimation("down", 1);
this.AddAnimation("up", 2);
this.AddAnimation("right", 3);
this.AddAnimation("left", 4);
this.AddAnimation("upright", 5);
this.AddAnimation("upleft", 6);
this.AddAnimation("downright", 7);
this.AddAnimation("downleft", 8);
commonBulletList = new List<CommonBullet>();
bulletDelay = 5f;
}
public void Update(GameTime gt)
{
timeElapsed += (float)gt.ElapsedGameTime.TotalSeconds;
if (!this.isAnimating)
{
if (timeElapsed > loadTime)
{
timeElapsed -= loadTime;
if (frameIndex == rects.Length - 1) this.isAnimating = false;
else frameIndex++;
}
}
kbs = Keyboard.GetState();
GetMovement();
if (kbs.IsKeyDown(Keys.Space)) Attack();
UpdateBullet();
}
#endregion
#region Method Region
private void GetMovement()
{
if (kbs.IsKeyDown(Keys.W))
{
this.pos.Y -= speed;
this.animation = "up";
if (kbs.IsKeyDown(Keys.D))
{
if (!isDirectional)
{
frameIndex = 0;
isDirectional = true;
}
this.pos.X += speed;
this.animation = "upright";
}
else if (kbs.IsKeyDown(Keys.A))
{
if (!isDirectional)
{
frameIndex = 0;
isDirectional = true;
}
this.pos.X -= speed;
this.animation = "upleft";
}
if (!this.isAnimating) this.isAnimating = true;
else this.isAnimating = false;
}
else if (kbs.IsKeyDown(Keys.S))
{
this.pos.Y += speed;
this.animation = "down";
if (kbs.IsKeyDown(Keys.D))
{
if (!isDirectional)
{
frameIndex = 0;
isDirectional = true;
}
this.pos.X += speed;
this.animation = "downright";
}
else if (kbs.IsKeyDown(Keys.A))
{
if (!isDirectional)
{
frameIndex = 0;
isDirectional = true;
}
this.pos.X -= speed;
this.animation = "downleft";
}
if (!this.isAnimating) this.isAnimating = true;
else this.isAnimating = false;
}
else if (kbs.IsKeyDown(Keys.A))
{
this.pos.X -= speed;
this.animation = "left";
if (!this.isAnimating) this.isAnimating = true;
else this.isAnimating = false;
}
else if (kbs.IsKeyDown(Keys.D))
{
this.pos.X += speed;
this.animation = "right";
if (!this.isAnimating) this.isAnimating = true;
else this.isAnimating = false;
}
else
{
this.isAnimating = false;
this.isDirectional = false;
frameIndex = 0;
}
if (this.pos.X <= 0) this.pos.X = 0;
if (this.pos.X >= (800 - this.width)) this.pos.X = 800 - this.width;
if (this.pos.Y <= 0) this.pos.Y = 0;
if (this.pos.Y >= (600 - this.height)) this.pos.Y = 600 - this.height;
}
private void Attack()
{
if (bulletDelay > 0) bulletDelay--;
if (bulletDelay <= 0)
{
CommonBullet cb = new CommonBullet();
if(cb.bulletTexture.Width > 0)
{
Console.WriteLine("this");
return;
}
cb.pos = new Vector2(this.pos.X - cb.bulletTexture.Width / 2, this.pos.Y + 15);
cb.hasFired = true;
if (commonBulletList.Count < bulletCap) commonBulletList.Add(cb);
bulletDelay = 0;
}
if (bulletDelay == 0) bulletDelay = shootDelay;
}
private void UpdateBullet()
{
foreach (CommonBullet cb in commonBulletList)
{
cb.box = new Rectangle((int)cb.pos.X, (int)cb.pos.Y, cb.bulletTexture.Width, cb.bulletTexture.Height);
cb.pos.Y -= cb.bulletSpeed;
if (cb.pos.Y < 0) cb.hasFired = false;
}
for (int i = 0; i < commonBulletList.Count; i++)
{
if (!commonBulletList[i].hasFired)
{
commonBulletList.RemoveAt(i);
i--;
}
}
}
public void Draw(SpriteBatch sb)
{
sb.Draw(texture, pos, animations[animation][frameIndex], Color.White, rot, ori, s, se, 0f);
foreach (CommonBullet cb in commonBulletList)
{
cb.Draw(sb);
}
}
#endregion
}
}
使用系统;
使用System.Collections.Generic;
使用System.Linq;
使用系统文本;
使用Microsoft.Xna.Framework;
使用Microsoft.Xna.Framework.Graphics;
使用Microsoft.Xna.Framework.Content;
使用Microsoft.Xna.Framework.Input;
命名空间SpaceShooter_Beta.Animation.PlayerCollection
{
公共类玩家:SpriteManager
{
#区域场区域
公共bool isAnimating=false,isDirectional=false;
专用浮动加载时间=0.05f,shootDelay,bulletDelay,timepassed;
公众浮标速度;
公共清单;
公共int Bullettcap=20;
键盘状态kbs;
#端区
#区域属性区域
公共int fps
{
设置{loadTime=(1f/值);}
}
#端区
#区域构造函数区域
公共播放器(纹理2D纹理、整数帧、整数动画):
基础(纹理、帧、动画)
{
速度=2f;
发射延迟=5f;
这个.AddAnimation(“down”,1);
这是AddAnimation(“up”,2);
这个.AddAnimation(“右”,3);
这个.AddAnimation(“左”,4);
这是AddAnimation(“直立”,5);
这个.AddAnimation(“upleft”,6);
这个.AddAnimation(“彻头彻尾”,7);
这个.AddAnimation(“downlight”,8);
commonBulletList=新列表();
延迟时间=5f;
}
公开作废更新(游戏时间gt)
{
时间经过+=(浮动)gt.ElapsedGameTime.TotalSeconds;
如果(!this.isAnimating)
{
如果(时间经过>加载时间)
{
时间经过-=加载时间;
如果(frameIndex==rects.Length-1)this.isAnimating=false;
else-frameIndex++;
}
}
kbs=Keyboard.GetState();
GetMovement();
if(kbs.IsKeyDown(Keys.Space))攻击();
UpdateBullet();
}
#端区
#区域法区域
私有移动()
{
if(kbs.IsKeyDown(Keys.W))
{
此.pos.Y-=速度;
this.animation=“up”;
if(kbs.IsKeyDown(键D))
{
if(!isDirectional)
{
frameIndex=0;
isDirectional=真;
}
这个.pos.X+=速度;
this.animation=“直立”;
}
else if(kbs.IsKeyDown(Keys.A))
{
if(!isDirectional)
{
frameIndex=0;
isDirectional=真;
}
此.pos.X-=速度;
this.animation=“upleft”;
}
如果(!this.isAnimating)this.isAnimating=true;
否则,该.isAnimating=false;
}
else if(kbs.IsKeyDown(Keys.S))
{
这个。pos.Y+=速度;
this.animation=“down”;
if(kbs.IsKeyDown(键D))
{
if(!isDirectional)
{
frameIndex=0;
isDirectional=真;
}
这个.pos.X+=速度;
this.animation=“downright”;
}
else if(kbs.IsKeyDown(Keys.A))
{
if(!isDirectional)
{
frameIndex=0;
isDirectional=真;
}
此.pos.X-=速度;
this.animation=“downlight”;
}
如果(!this.isAnimating)this.isAnimating=true;
否则,该.isAnimating=false;
}
else if(kbs.IsKeyDown(Keys.A))
{
此.pos.X-=速度;
this.animation=“left”;
如果(!this.isAnimating)this.isAnimating=true;
否则,该.isAnimating=false;
}
else if(kbs.IsKeyDown(Keys.D))
{
这个.pos.X+=速度;
this.animation=“right”;
如果(!this.isAnimating)this.isAnimating=true;
否则,该.isAnimating=false;
}
其他的
{
this.isAnimating=false;
this.isDirectional=false;
frameIndex=0;
}
如果(this.pos.X=(800-this.width))this.pos.X=800-this.width;
如果(this.pos.Y=(600-this.height))this.pos.Y=600-this.height;
}
私有无效攻击()
{
如果(bulletDelay>0)bulletDelay--;
如果(延迟0)
{
Console.WriteLine(“本”);
返回;
}
cb.pos=新矢量2(此.pos.X-cb.Width/2,此.pos.Y+15);
cb.hasFired=true;
如果(commonBulletList.Countusing System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Content;
namespace SpaceShooter_Beta
{
public class SpriteManager
{
#region Field Region
protected Texture2D texture;
protected Rectangle[] rects;
protected int frameIndex = 0, frames;
protected Dictionary<string, Rectangle[]> animations = new Dictionary<string, Rectangle[]>();
public Vector2 pos, ori;
public float rot = 0f, s= 1f;
public SpriteEffects se;
public string animation;
public int width, height;
#endregion
#region Property Region
#endregion
#region Constructor Region
public SpriteManager(Texture2D texture, int frame, int animation)
{
this.texture = texture;
this.frames = frame;
rects = new Rectangle[frame];
for (int i = 0; i < frame; i++)
{
rects[i] = new Rectangle(i * width, 0, width, texture.Height);
}
width = texture.Width / frame;
height = texture.Height / animation;
}
#endregion
#region Method Region
public void AddAnimation(string name, int row)
{
Rectangle[] rects = new Rectangle[frames];
for (int i = 0; i < frames; i++)
{
rects[i] = new Rectangle(i * width, (row - 1) * height, width, height);
}
animations.Add(name, rects);
}
#endregion
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Content;
namespace SpaceShooter_Beta.Animation.PlayerCollection
{
public class CommonBullet
{
#region Field Region
public float bulletSpeed = 5f;
public double bulletDamage;
public Texture2D bulletTexture;
public Vector2 pos, ori;
public bool hasFired;
public Rectangle box;
#endregion
#region Property Region
#endregion
#region Constructor Region
public CommonBullet()
{
hasFired = false;
}
#endregion
#region Method Region
public void Draw(SpriteBatch sb)
{
sb.Draw(bulletTexture, pos, Color.White);
}
public void LoadContent(ContentManager Content)
{
bulletTexture = Content.Load<Texture2D>(@"playerbullet");
}
#endregion
}
}
CommonBullet cb = new CommonBullet();
cb.LoadContent(your content manager)
CommonBullet.LoadContent(this.ContentManager);