1

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つの座標が原点に向かってループをカウントダウンするのを見ることができます.これは、ベクトルをゼロにしてその座標をベクトルに再適用しても発生します(ベクトル2がその情報を保持している場合でも、大きさを削除するだけだと思います) )。助けてくれてありがとう!

// 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
4

1 に答える 1

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プロパティをスプライトのクラスに追加できます。

于 2012-05-26T15:51:28.363 に答える