我是新来的和编程。我一直在寻找,虽然我找不到任何可以解决问题的方法。
我正在尝试使我的spritesheet循环通过我的spritesheet的不同步行框架,我已经使用IsKeyDown轻松完成了它但是当使用鼠标走到某个地方时我花了一段时间来坚持一个'坏' '解决方案:
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;
它有点奏效,但想知道是否有更好的解决方法。 我想要的是能够点击游戏画面并选择相对于精灵当前位置和目的地的相应精灵行,以使其以正确的方式制作动画。
很抱歉,如果之前有人询问过这个问题,但是在发布此内容之前我已经搜索了几个小时但没有找到任何内容。
答案 0 :(得分:0)
我有点不清楚你到底在做什么。你只有2个精灵,一个用于左边,一个用于右边?这就是我在你当前的代码中可以看到的所有东西,但你指的是动画。我将假设您有一整套精灵来制作步行动画,在这种情况下,我认为本教程将涵盖您需要的内容:
http://coderplex.blogspot.ca/2010/04/2d-animation-part-1-basics.html
(更具体地说,教程的第4部分:http://coderplex.blogspot.ca/2010/04/2d-animation-part-4-sprite-animation.html)
基本上,您需要设置定时器来控制精灵动画,就像这种类型的鼠标移动一样,在您单击鼠标和对象到达时间之间不再有输入(与移动相关)目的地。因此,您需要使用计时器来确定何时应该调用步行动画中的下一个精灵。
或者,您可以进一步详细说明您的if语句(如果currentFrame = 1则currentFrame = 2,如果currentFrame = 2则currentFrame = 3等),但如果您进行更改,它将会很混乱并且很难维护从spritesheet中拉出精灵的图形或方式。它也很可能动画太快,无论如何你都必须使用计时器来降低速度。
答案 1 :(得分:0)
我想是想通了。这是我的代码(希望它的格式正确。抱歉,如果我不想回答我自己的问题,认为这可能对其他人有帮助):
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();
}