C# 无法在构造函数中设置纹理
我的源C# 无法在构造函数中设置纹理,c#,xna,C#,Xna,我的源矩形有问题,因此我的纹理不会显示在屏幕上。 当我使用源为null的Draw方法时,纹理可以工作 我不知道这是怎么回事 另外,如果我将其放入构造函数:source=new Rectangle((int)position.x,(int)position.Y,texture.Width/frameas,texture.Height)。我得到了错误 “使用new关键字创建对象” 我的游戏1中肯定没有错误,因为我只在其中加载纹理、更新和绘制 public class Player { pub
矩形有问题,因此我的纹理不会显示在屏幕上。
当我使用源为null的Draw方法时,纹理可以工作
我不知道这是怎么回事
另外,如果我将其放入构造函数:source=new Rectangle((int)position.x,(int)position.Y,texture.Width/frameas,texture.Height)
。我得到了错误
“使用new关键字创建对象”
我的游戏1中肯定没有错误,因为我只在其中加载纹理、更新和绘制
public class Player
{
public Texture2D texture;
public Vector2 position;
public int speed, width,frames, jump;
public float scale;
public Vector2 velocity;
public float gravity;
public bool hasJumped;
public Rectangle source;
public Player(int x, int y)
{
speed = 5;
position.X = x;
position.Y = y;
scale = 1.8f;
frames = 4;
source = new Rectangle(x,y, 30,30);
}
public void LoadContent(ContentManager Content)
{
texture = Content.Load<Texture2D>("player");
}
public void Update(GameTime gameTime)
{
position += velocity;
KeyboardState keyState = Keyboard.GetState();
if (keyState.IsKeyDown(Keys.D))
{
velocity.X = 3f;
}
if (keyState.IsKeyDown(Keys.A))
{
velocity.X = -3f;
}
if (keyState.IsKeyDown(Keys.Space) && hasJumped==false)
{
position.Y -= 10f;
velocity.Y = -5f;
hasJumped = true;
}
if (hasJumped == true)
velocity.Y += 0.15f;
else
velocity.Y = 0f;
}
public void Draw(SpriteBatch spriteBatch)
{
spriteBatch.Draw(texture, position, source, Color.White, 0f, Vector2.Zero, scale, SpriteEffects.None, 0f);
}
}
}
公共类播放器
{
公共纹理2D纹理;
公共向量2位置;
公共整数速度、宽度、帧数、跳跃;
公众持股比例;
公共矢量2速度;
公众浮力;
公众的注意力已经跳跃;
公共矩形源;
公共播放器(整数x,整数y)
{
速度=5;
位置X=X;
位置Y=Y;
刻度=1.8f;
帧=4;
源=新矩形(x,y,30,30);
}
公共void加载内容(ContentManager内容)
{
纹理=内容。加载(“播放器”);
}
公开作废更新(游戏时间游戏时间)
{
位置+=速度;
KeyboardState keyState=Keyboard.GetState();
if(键状态IsKeyDown(键D))
{
速度X=3f;
}
if(keyState.IsKeyDown(Keys.A))
{
速度X=-3f;
}
if(keyState.IsKeyDown(Keys.Space)和&hasshopped==false)
{
位置Y-=10f;
速度Y=-5f;
hasshopped=true;
}
if(hasshopped==true)
速度Y+=0.15f;
其他的
速度Y=0f;
}
公共作废抽签(SpriteBatch SpriteBatch)
{
spriteBatch.Draw(纹理、位置、源、颜色、白色、0f、矢量2、零、比例、SpriteEffects.None、0f);
}
}
}
您不能在构造函数中引用纹理,因为它还不存在。在LoadContent()
中加载纹理之前,它不会设置为实际值,因此当您尝试使用它构建矩形时,它会引发NullReferenceException
在此行后创建源矩形:
texture = Content.Load<Texture2D>("player");
texture=Content.Load(“播放器”);
您能否提供更多关于您在构造函数中使用new
关键字描述的错误的详细信息?另外,请在玩家构造器中发布玩家纹理的大小和你期望的(以及你实际得到的)源矩形。源=新矩形((int)position.x,(int)position.Y,(int)texture.Width/frameas,(int)texture.Height)@JackGajanan,可以在问题中找到,我问了一些关于它的细节。如果我让矩形比纹理本身大,可能会有问题吗?(我不这么认为)。关于这个新的关键字错误,只有当我更改行源=新矩形(x,y,30,30)时才会发生;当source=new Rectangle((int)position.x,(int)position.Y,texture.Width/frameas,texture.Height)时,这一点是正确的,但它仍然不会在屏幕上显示纹理。假设我的纹理(瓷砖)大小为100/50,其中有两帧,我想显示第一帧。我创建了:Rectangle source=new Rectangle((int)position.X,(int)position.Y,texture.Width/frames,texture.Height)(就像我在上面的代码中所做的那样),它没有在屏幕上显示纹理,这很奇怪,因为它应该可以工作位置的值是多少?你似乎在用它来表示两个不同的东西:精灵在纹理上的位置和玩家在屏幕上的位置。位置=玩家在屏幕上的位置。。假设是(100100),那么您创建的源矩形不正确。源矩形的x,y坐标应该是精灵在纹理上的位置。如果你将它们设置为0,0会发生什么?哇,现在它工作了非常感谢你,现在我明白了。我认为这是一个球员的位置坐标,而不是纹理中的精灵位置。再一次thx