1
我有在获得精灵的左上角坐标的麻烦。我试图用texture2d.bounds既bounds.location.x \ y或bounds.left \顶部...但由于某些原因,这些参数为:x = 0,Y = 0和对象的移动不会改变它(甚至如果起点不是0,0)。 我该如何解决这个问题?XNA得到一个精灵的左上角坐标
我有在获得精灵的左上角坐标的麻烦。我试图用texture2d.bounds既bounds.location.x \ y或bounds.left \顶部...但由于某些原因,这些参数为:x = 0,Y = 0和对象的移动不会改变它(甚至如果起点不是0,0)。 我该如何解决这个问题?XNA得到一个精灵的左上角坐标
Texture2D.Bounds
获取资源的大小,对MSDN document所见。该X
和Y
位置将始终为0,你永远需要他们,为Bounds
的唯一原因是让Width
和纹理的Height
。
就是这样说的,Texture2D
只是代表一个纹理,他们做不是代表一个位置。你需要做的是使用一个二维向量Vector2
来定位你的精灵。
例子:
Texture2D sprite;
Vector2 position;
...
protected override void Draw(GameTime gameTime)
{
spriteBatch.Begin();
spriteBatch.Draw(sprite, position, Color.White)
spriteBatch.End();
}
创建每个精灵的位置和纹理的相反,你可以做一个单独的精灵类来处理你的位置,质地,和绘图。
public class Sprite
{
public Texture2D Texture { get; set; }
public Vector2 Position { get; set; }
public Sprite(Texture2D texture, Vector2 initialPosition)
{
Texture = texture;
Position = initialPosition;
}
public virtual void Draw(SpriteBatch spriteBatch, GameTime gameTime)
{
spriteBatch.Draw(Texture, Position, Color.White);
}
}
一个已经绘制的精灵?不会发生。为什么你的精灵不是用带有位置的对象表示的(然后在该位置绘制精灵),所以你已经知道它在哪里了? – BradleyDotNET