在xna我有一個sprite的位置vector2,當引用sprite繪製的座標時。 (現在不考慮z,這是基於屏幕位置正確堆疊精靈的失敗嘗試)。當手動引用vector2座標時,我總是看起來最終在原點,請看我的意思。in xna如何正確引用與原點相關的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向量座標繪製精靈,一切都是hunky dory。如果我嘗試以我能想到的任何其他方式手動使用這些相同的座標,座標始終似乎最終成爲原點(左上角)。除此之外,一切工作順利,和精靈的運動是細
//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個座標向下朝向原點向下倒數,這種情況發生,即使我將矢量調零並將其座標重新應用到矢量(我假設vector只是刪除任何數量級的矢量,如果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位置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
什麼是'_x'和'_y'?他們與「職位」有什麼不同?你是否每幀計算目的地? 「moveTo」看起來像什麼? –
我對原始文章做了一些修改,希望能夠澄清一些混淆,因爲當精靈引用它自己的vector2座標時,您會發現問題正在發生。感謝您的關注! – scape