在 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");
}
}
所以在使用屏幕坐标时,精灵会很好地移动到目的地;但是如果我使用精灵自己的向量-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