私はそれを考え出した。これが私のコードです(正しくフォーマットされていることを願っています。自分の質問に答えるつもりがない場合は申し訳ありませんが、これは他の誰かに役立つかもしれないと思いました):
public Vector2 position = new Vector2(200, 200);
Point frameSize = new Point(48, 92);
Point currentFrame = new Point(0, 0);
Point sheetSize = new Point(9, 8);
float speed = 10;
Vector2 direction;
Vector2 destination;
bool mousePressed = false;
float difference;
KeyboardState currentState;
KeyboardState theKeyboardState;
KeyboardState oldKeyboardState;
enum State
{
Walking
}
State mcurrentState = State.Walking;
TimeSpan nextFrameInterval =
TimeSpan.FromSeconds((float)1 / 16);
TimeSpan nextFrame;
MouseState mouseState;
MouseState oldState;
public void Move()
{
direction = destination - position;
direction.Normalize();
position += direction * speed;
float Xdistance = destination.X - position.X;
float Ydistance = destination.Y - position.Y;
difference = (float)Math.Atan2(Ydistance, Xdistance);
float differ;
differ = MathHelper.ToDegrees(difference);
if (destination.X >= position.X || destination.X <= position.X)
{
currentFrame.X++;
if (currentFrame.X >= 9)
currentFrame.X = 0;
//down = 90dg
if (differ >= 67.6 && differ <= 112.5)
currentFrame.Y = 0;
if (differ >= 112.6 && differ <= 157.5)
currentFrame.Y = 1;
if (differ >= 157.6 && differ <= 180 || differ >= -180 && differ <= -157.5)
currentFrame.Y = 2;
if (differ >= -157.4 && differ <= -112.5)
currentFrame.Y = 3;
if (differ >= -112.4 && differ <= -67.5)
currentFrame.Y = 4;
if (differ >= -67.4 && differ <= -22.5)
currentFrame.Y = 5;
if (differ >= -22.4 && differ <= 22.5)
currentFrame.Y = 6;
if (differ >= 22.6 && differ <= 67.5)
currentFrame.Y = 7;
}
}
public void Update()
{
mouseState = Mouse.GetState();
currentState = Keyboard.GetState();
theKeyboardState = Keyboard.GetState();
if (mousePressed == true)
{
if (Vector2.DistanceSquared(destination, position) >= speed * speed)
{
Move();
}
}
if (mouseState.LeftButton == ButtonState.Pressed && oldState.LeftButton == ButtonState.Released)
{
int mouseY = mouseState.Y;
int mouseX = mouseState.X;
destination = new Vector2(mouseX, mouseY);
mousePressed = true;
}
oldState = mouseState;
if (mcurrentState == State.Walking)
{
#region KB animation
if (currentState.IsKeyDown(Keys.Down))
{
mousePressed = false;
currentFrame.X++;
currentFrame.Y = 0;
if (currentFrame.X >= 9)
currentFrame.X = 0;
position.Y += speed;
}
if (currentState.IsKeyDown(Keys.Up))
{
mousePressed = false;
currentFrame.X++;
currentFrame.Y = 4;
if (currentFrame.X >= 9)
currentFrame.X = 0;
position.Y -= speed;
}
if (currentState.IsKeyDown(Keys.Right))
{
mousePressed = false;
currentFrame.X++;
currentFrame.Y = 6;
if (currentState.IsKeyDown(Keys.Down))
currentFrame.Y = 7;
if (currentState.IsKeyDown(Keys.Up))
currentFrame.Y = 5;
if (currentFrame.X >= 9)
currentFrame.X = 0;
position.X += speed;
}
if (currentState.IsKeyDown(Keys.Left))
{
mousePressed = false;
currentFrame.X++;
currentFrame.Y = 2;
if (currentState.IsKeyDown(Keys.Down))
currentFrame.Y = 1;
if (currentState.IsKeyDown(Keys.Up))
currentFrame.Y = 3;
if (currentFrame.X >= 9)
currentFrame.X = 0;
position.X -= speed;
}
}
oldKeyboardState = theKeyboardState;
#endregion
}
public void Draw(SpriteBatch spriteBatch, Texture2D character)
{
spriteBatch.Begin();
spriteBatch.Draw(character, position, new Rectangle(frameSize.X * currentFrame.X,
frameSize.Y * currentFrame.Y, frameSize.X, frameSize.Y), Color.White, 0, new Vector2(frameSize.X / 2, frameSize.Y / 2), 1, SpriteEffects.None, 0);
spriteBatch.End();
}