2011-08-21 47 views
2

这是我在本节的第一篇文章(XNA和游戏开发)。我正在尝试获取下面的图片。正如你所看到的,有一条高速公路,里面有一些物体在移动(毫秒)。我猜streeth行为就像一个管道。当高速公路载入一个物体时,它会出现在开始处,它将穿过高层软件,直到它到达高速公路的另一个极端。我需要为此游戏创建此控件吗? (像管道)

我的主要问题是,如何才能在公路内部移动多个物体?

enter image description here

在此先感谢。

回答

0

你需要点名单和精灵列表

class Path 
{ 
    List<Vector2> Points; 
    float[] Lengths; 
    Vector2[] Directions; 

    void Build() 
    { 
     Lengths = new float[Points.Count-1]; 
     Directions = new float[Points.Count-1]; 
     for (int i=0; i<Points.Count-1;i++) 
     { 
      Directions[i] = Points[i+1] - Points[i]; 
      Lengths[i] = Directions[i].Length(); 
      Directions[i].Normalize(); 
     } 
    } 
} 

class Sprite 
{ 
    Vector2 Position; 
    float StagePos; 
    int StageIndex; 
    Path Path; 
    float Speed; 

    void Update(float Seconds) 
    { 
     if (StageIndex!=Path.Points.Count-1) 
     { 
      StagePos += Speed * Seconds; 
      while (StagePos>Path.Lengths[StageIndex]) 
      { 
       StagePos -= Path.Lengths[StageIndex]; 
       StageIndex++;    
       if (StageIndex == Path.Points.Count-1) 
       { 
        Position = Path.Points[StageIndex]; 
        return; 
       } 
      } 
      Position = Path.Points[StageIndex] + Directions[StageIndex] * StagePos; 
     } 
    }  
}