2014-12-03 70 views
0

我正在为学习目的制作2D塔防游戏,现在我陷入了如何让移动中的敌人看向正确的方向。如何检测精灵在哪个方向移动?

这是我尝试没有成功:

// Class constructor 
    public AnimatedSprite(Texture2D texture, int lines, int columns, Vector2 position) 
     : base (texture, position) 
    { 
     this.texture = texture; 
     this.position = position; 
     Lines = lines; // How many lines the sprite-sheet have 
     Columns = columns; How many columns the sprite-sheet have 
     totalFrames = Lines * Columns; 
    } 

这里的更新方法,它是一个5x4的精灵表:

public override void Update(GameTime gameTime) 
    { 

     // Depending on the direction the sprite is moving 
     // it will use different parts of the sprite-sheet 

     if (west == true) 
     { 
      initialFrame = 0; 
      finalFrame = 4; 
     } 

     if (east == true) 
     { 
      initialFrame = 5; 
      finalFrame = 9; 
     } 

     if (north == true) 
     { 
      initialFrame = 10; 
      finalFrame = 14; 
     } 

     if (south == true) 
     { 
      initialFrame = 15; 
      finalFrame = 19; 
     } 

     // When to update the current frame 
     timeSinseLastFrame += gameTime.ElapsedGameTime.Milliseconds; 
     if (timeSinceLastFrame > milisecondsPerFrame) 
     { 
      timeSinceLastFrame -= milisecondsPerFrame; 
      currentFrame++; 

     // Reset the frame to complete the loop 
      if (currentFrame == finalFrame) 
       currentFrame = initialFrame; 
     } 
    } 

在Draw方法我试图侦测精灵所面临的位置,我不确定最好的办法,但它不工作,敌人绘制正确和动画,但有时沿着地图路径面对错误的方向,并在某一点他们消失:

public override void Draw(SpriteBatch spriteBatch) 
    { 
     int width = texture.Width/Columns; 
     int height = texture.Height/Lines; 
     int line = (int)((float)currentFrame/(float)Columns); 
     int column = currentFrame % Columns; 

     Rectangle originRectangle = new Rectangle(width * column, height * line, width, height); 
     Rectangle destinationRectangle = new Rectangle((int)position.X, (int)position.Y, width, height); 


     // Heres what i think it's not correct, how to detect the direction 
     // I tried to calculate it by the last X and Y position 
     // So if the current Y value is smaller the last Y value 
     // The sprite moved south 
     Rectangle lastPosition = originRectangle; 
     Rectangle destinationPosition = destinationRectangle; 

     if (lastPosition.Y < destinationPosition.Y) 
     { 
      north = true; 
      south = false; 
      east = false; 
      west = false; 
     } 

     if (destinationRectangle.Y > lastPosition.Y) 
     { 
      north = false; 
      south = true; 
      east = false; 
      west = false; 
     } 

     if (destinationRectangle.X > lastPosition.X) 
     { 
      north = false; 
      south = false; 
      east = true; 
      west = false; 
     } 

     if (destinationRectangle.X < lastPosition.X) 
     { 
      north = false; 
      south = false; 
      east = false; 
      west = true; 
     } 

     spriteBatch.Draw(texture, destinationRectangle, originRectangle, Color.White); 

    } 
} 
} 
+1

你还没有真正问了一个问题在这里;并且有很多代码(比没有代码好!)。你能缩小一切吗?根据给定的输入指出没有做到你想要的行/部分(提供实际/预期的输出)? – BradleyDotNET 2014-12-03 18:18:29

+0

如果此代码是与此问题相关的所有内容,那么您似乎需要设计具有FacingDirection的角色概念。似乎没有任何关系。一旦你设计好了,你可以更新Draw方法来根据这些数据来操作图像/绘图。 – 2014-12-03 18:54:11

+0

我更新了这个问题,并在代码上加了几条评论来澄清。 – ViNi 2014-12-03 19:35:22

回答

0

我解决了它,我从基类中选择位置并更新基类中的最后一个位置,而在敌人类中,更新方法将根据位置处理它将使用的框架。

敌对阶级更新方法:

  if (lastPosition.Y < position.Y) 
      { 
       initialFrame = 15; 
       lastFrame = 19; 
      } 

      if (position.Y < lastPosition.Y) 
      { 
       initialFrame = 10; 
       lastFrame = 14; 
      } 

      if (position.X > lastPosition.X) 
      { 
       initialFrame = 5; 
       lastFrame = 9; 
      } 

      if (position.X < lastPosition.X) 
      { 
       initialFrame = 0; 
       lastFrame = 4; 
      } 

      currentFrame = initialFrame; 
      totalFrame = lastFrame; 

      timeSinceLastFrame += gameTime.ElapsedGameTime.Milliseconds; 
      if (timeSinceLastFrame > milisecondsPerFrame) 
      { 
       timeSinceLastFrame -= milisecondsPerFrame; 
       currentFrame++; 
       if (currentFrame == totalFrame) 
        currentFrame = 0; 
      }  
     } 
0

在你的代码的THI段第一外表:

if (lastPosition.Y < destinationPosition.Y) 
{ 
    north = true; 
    south = false; 
    east = false; 
    west = false; 
} 

if (destinationRectangle.Y > lastPosition.Y) 
{ 
    north = false; 
    south = true; 
    east = false; 
    west = false; 
} 

的(lastPosition.Y < destinationPosition.Y)和(destinationRectangle.Y> lastPosition.Y) 是相同的条件,所以你的在两种情况下都会有南=真。

其次,你为什么使用四个布尔变量?而是使用一个具有四种可能状态的变量。枚举对此很有用:

enum Direction 
{ 
    None = 0, // default 
    North = 1, 
    South = 2, 
    East = 3, 
    West = 4 
} 

而不是四个布尔值使用一个Direction变量。较少的变量是较少的麻烦。

最后。 我无法正确理解你如何移动你的精灵?我的意思是如何在更换坐标之前检测方向? 在我看来,它应该通过这样的:

  1. 在更新()在你所需要的方向移动,并改变方向varialbe和基础上改变和更新精灵状态检测。
  2. 在Draw()中,在当前状态下绘制精灵。
+0

嘿,谢谢你的回答。我已经解决了这个问题,并且让我更简单,没有变数。 – ViNi 2014-12-04 16:53:10