在 xna i 中, 有一种带有位置矢量 2 的图示, 当引用图示绘制的坐标时 。 (目前, 忽略 z, 这是根据屏幕位置正确堆放图示的尝试失败 。) 当手动引用矢量2 坐标时, 我似乎总是在来源处结束, 请看我是什么意思 。
//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);
在绘制图案时,所有的东西都是hunky dory, 并参照其 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");
}
}
so while using screen coordinates the sprite moves fine to the destination; but what happens if i use the sprite s own vector-x and y coordinates as part of its destination is that over time the sprite ends up in the top left corner, like destiny. why does this happen? it happens when referencing the coordinates by hand, in other ways too, such as collision detection. I can actually see the 2 coordinates count down over the loop down towards origin, this happens even if i zero out the vector and reapply it s coordinates back to the vector (i assume that does nothing but remove any magnitude, if vector2 even holds that information). thanks for the help!
// 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
有趣的是,如果我指定另一个图示矢量2位置的coords, 似乎效果不错:
//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