Animation 通过单击移动更改子画面的Y轴
我是新来的,不熟悉编程。我已经找了一段时间了,虽然我找不到任何有助于解决这个问题的东西 我试图让我的精灵表在精灵表的不同行走帧之间循环,我用IsKeyDown很容易做到,但当涉及到使用鼠标在某处行走时,我花了一段时间才想出一个“糟糕”的解决方案:Animation 通过单击移动更改子画面的Y轴,animation,xna,mouse,sprite-sheet,Animation,Xna,Mouse,Sprite Sheet,我是新来的,不熟悉编程。我已经找了一段时间了,虽然我找不到任何有助于解决这个问题的东西 我试图让我的精灵表在精灵表的不同行走帧之间循环,我用IsKeyDown很容易做到,但当涉及到使用鼠标在某处行走时,我花了一段时间才想出一个“糟糕”的解决方案: if (destination.X > position.X) currentFrame.Y = 6; if (destination.X > posi
if (destination.X > position.X)
currentFrame.Y = 6;
if (destination.X > position.X && destination.Y >= position.Y + 35)
currentFrame.Y = 7;
if (destination.X > position.X && destination.Y <= position.Y - 35)
currentFrame.Y = 5;
这算是可行的,但我想知道是否有更好的解决办法。
我想要的是能够在游戏屏幕上点击,并选择适当的精灵行,相对于精灵当前位置和目的地,使其以正确的方式设置动画
很抱歉,如果以前有人问过这个问题,但我在发布之前已经搜索了几个小时,没有发现任何东西。我有点不清楚您到底在做什么。你只有两个精灵吗,一个左一个右?这是我在您当前代码中看到的所有内容,但您指的是动画。我将假设您有一整套精灵来设置行走动画,在这种情况下,我认为本教程将涵盖您需要的内容: 更具体地说,本教程第4部分: 基本上,您需要设置计时器来控制精灵动画,因为对于这种类型的鼠标移动,从单击鼠标到对象到达目标之间的移动没有更多的输入。因此,需要使用计时器来确定何时调用行走动画中的下一个精灵
或者,如果currentFrame=1,那么currentFrame=2,如果currentFrame=2,那么currentFrame=3,等等,您可以进一步详细说明您的if语句,但是如果您对图形或精灵从精灵表中拉出的方式进行更改,这将是混乱的,并且很难维护。它也很可能动画太快,而且你必须使用定时器来减慢它的速度。我想我已经解决了。这是我的代码,希望格式正确。抱歉,如果我不打算回答我自己的问题,我想这可能会对其他人有所帮助:
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();
}
是的,我不太确定该怎么说。我的精灵表是[9,8],有8个运动方向和9帧动画。这是我的一个键盘;错过了5分钟的编辑规则,并改变了我的解释一点。是的,我不太确定该怎么说。我的精灵表是[9,8],有8个运动方向和9帧动画。我试图找到的是,如果我点击我的精灵的北部,如何使我的精灵表更改为北部行,我让它在9个不同的精灵之间循环,只是不容易通过鼠标坐标更改精灵的行。我会查看链接并感谢您的回复。