在xna中,当引用精灵绘图的坐标时,我有一个带位置vector2的精灵。 (暂时忽略z,这是根据屏幕位置正确堆叠精灵的失败尝试)。当手动引用vector2坐标时,我似乎总是在原点,请看看我的意思。
//draw sprite, this is called repeatedly during gameplay, it's inside a draw function
theGame.spriteBatch.Draw(headings[heading][frame], new Rectangle((int)theSprite.position.X, (int)theSprite.position.Y, theSprite.width, theSprite.height), null, Color.White, 0f, Vector2.Zero, SpriteEffects.None, theSprite.z);
通过引用它的X和Y矢量坐标来绘制精灵时,一切都是笨拙的。如果我尝试以我能想到的任何其他方式手动使用那些相同的坐标,坐标似乎总是最终作为原点(左上角)。除此之外,一切都顺利进行,精灵的运动很好
//the movement code for the particular sprite:
// this is a function i call repeatedly during gameplay, it moves the sprite
public void Move(GameTime Time)
{
if (!move) { return; } //not even moving? exit function
direction = destination - position;//these are all vector2 orbjects
//speed is a float
position += direction * (speed * (float)Time.ElapsedGameTime.TotalSeconds);
syncBounds();
if (Vector2.Distance(position,destination)<bounds_sphere.Radius) //close enough? stop moving
{
move = false;
}
}
//here's the moveTo function which triggers the move-event, i typically call it with screen coordinates
public void moveTo(float _x, float _y, string _anim)
destination = new Vector2(_x - (width / 2), _y - (height / 2));
curr_anim = _anim;
move = true; //kicks off trigger
}
//here's an example of moveTo working with basic coordinates
// Process touch events
TouchCollection touchCollection = TouchPanel.GetState();
foreach (TouchLocation tl in touchCollection)
{
if ((tl.State == TouchLocationState.Pressed))//|| (tl.State == TouchLocationState.Moved))
{
float px = tl.Position.X; //touch x
float py = tl.Position.Y; //touch y
gameObjects.Sprites[player].moveTo(px, py, "running");
}
}
所以在使用屏幕坐标时,精灵可以很好地移动到目的地;但是如果我使用精灵自己的vector-x和y坐标作为其目标的一部分会发生什么呢?随着时间的推移,精灵最终会出现在左上角,就像命运一样。 为什么会这样?当以其他方式手动参考坐标时,例如碰撞检测,就会发生这种情况。我实际上可以看到2个坐标在循环向下朝向原点倒计时,即使我将向量归零并将其坐标重新应用回向量,也会发生这种情况(我假设除了删除任何幅度之外什么都不做,如果vector2甚至保留了该信息)。谢谢你的帮助!
// i don't think this actually does anything
vector2 savepos = position;
position = Vector2.Zero;
position.x = savepos.x;
position.y = savepos.y;
//example of my code failing when referencing the vector2 coordinates
//sprite (me) auto-flee function code
int x = (int) me.position.X;
int y = (int) me.position.Y;
int dist = 2;
me.moveTo(rand.Next(x-dist,x+dist), rand.Next(y-dist, y+dist), running); //kick off moveTo function, begin the animation sequence
有趣的是,如果我指定另一个精灵的vector2位置坐标,它似乎工作正常:
//this works and the sprites move to the other object
me.moveTo(gameObjects.Sprites["anotherspriteobject"].position.X, gameObjects.Sprites["anotherspriteobject"].position.Y, running); //kick off moveTo function, begin the animation sequence
答案 0 :(得分:0)
如果您经常使用这些参数调用moveTo函数,则精灵自然会在左上角结束。精灵的位置成员是它的左上角。但是,在moveTo函数中,您期望中心的位置。这就是你减去一半大小的原因。因此,当您将精灵的中心永久移动到其左上角时,当您阻止精灵离开时,它将导致屏幕的左上角。
解决方案是使用精灵实际中心点作为参考:
int x = (int) me.position.X + me.width/2;
int y = (int) me.position.Y + me.height/2;
int dist = 2;
me.moveTo(rand.Next(x-dist,x+dist), rand.Next(y-dist, y+dist), running); //kick off moveTo function
因此,您可以将一个CenterPosition属性添加到精灵的类中。