あなたが私を助けてくれることを願っています。
ユーザーが次のように右ボタンをクリックすると、マウスの位置に応じてスプライトが移動します。
protected override void Update(GameTime gameTime)
{
int nextX = SpritePosition.X;
int nextY = SpritePosition.Y;
int SpriteWidth = 135;
int SpriteHeight = 135;
int Speed = 3;
MouseState ms = Mouse.GetState();
if (ms.RightButton == ButtonState.Pressed)
{
if (ms.X > SpritePosition.X + SpriteWidth) //check to move right
{
nextX = SpritePosition.X + Speed;
}
else if (ms.X < SpritePosition.X) //check to move left
{
nextX = SpritePosition.X - Speed;
}
if (ms.Y > SpritePosition.Y + SpriteHeight) //Check to move bottom
{
nextY = SpritePosition.Y + Speed;
}
else if (ms.Y < SpritePosition.Y) //Check to move top
{
nextY = SpritePosition.Y - Speed;
}
//Change the Sprite position to be updated in the DRAW.
SpritePosition = new Rectangle(nextX, nextY, graphics.GraphicsDevice.Viewport.Width, graphics.GraphicsDevice.Viewport.Height);
}
base.Update(gameTime);
}
現在は機能していますが、移動方法は次のようになります。
間違った移動http://i.imgur.com/xGsFy38.png
私が望む方法:右に動くhttp://i.imgur.com/kkEnoYD.png
みんな、今私は以下の答えから、以下を試しました:
Vector2 From = new Vector2(SpritePosition.X, SpritePosition.Y);
Vector2 To = new Vector2(ms.X, ms.Y);
From = Vector2.Subtract(From,To );
Vector2 Direction = Vector2.Normalize(From);
Direction = Direction * Speed;
SpritePosition = new Vector2(Direction.X, Direction.Y);
私のスプライトは動いていません、何が間違っているのですか?