我遇到源Rectangle
问题,并且因此我的纹理不显示在屏幕上。 当我使用绘图方法与源为null纹理的作品。无法在构造函数中设置纹理
我不知道这有什么问题。
此外,如果我把这个到构造函数:source=new Rectangle((int)position.x,(int)position.Y, texture.Width/frameas, texture.Height)
。我得到的错误
“使用新关键字来创建一个对象”
有一个在我的Game1没有错误肯定,因为我只装质地,更新,并绘制在那里。
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);
}
}
}
请您提供您在构造函数中'new'关键字描述错误的一些更详细?也请在播放器构造函数中发布播放器纹理的大小以及您期望得到的源矩形(以及您实际获得的内容)的大小。 – user1306322 2013-02-26 12:51:27
源=新Rectangle((int)position.x,(int)position.Y,(int)texture.Width/frameas,(int)texture.Height) – 2013-02-26 13:07:39
@JackGajanan可以在问题中找到,我问为**一些更详细的**。 – user1306322 2013-02-26 13:14:07