2012-12-03 35 views
0

您好我有这些向量:列表需要一些基础知识

Vector2 ground1p1 = new Vector2(0,430); 
    Vector2 ground1p2 = new Vector2(200,430); 
    Vector2 ground1p3 = new Vector2(0, 290); 
    Vector2 ground1p4 = new Vector2(280, 340); 

我想将它们放在一个列表,这样,而不是这样:

if (DetectPlayerAndGround1Collision2(playerPosition,ground1p1,player,ground1) == true) 
      { 
       hasJumped = false; 
       velocity.Y = 0f; 
      } 
      if (DetectPlayerAndGround1Collision2(playerPosition, ground1p2, player, ground1) == true) 
      { 
       hasJumped = false; 
       velocity.Y = 0f; 
      } 

这里当我在写作结束时写入“矢量”时,问题没有发生,就像我没有声明的那样:

public class Game1 : Microsoft.Xna.Framework.Game 
    { 

     GraphicsDeviceManager graphics; 
     SpriteBatch spriteBatch; 




     public Game1() 
     { 
      graphics = new GraphicsDeviceManager(this); 
      Content.RootDirectory = "Content"; 
     } 


     protected override void Initialize() 
     { 

      base.Initialize(); 
     } 


     bool hasJumped = true; 


     Vector2 velocity; 
     Texture2D player; 
     Texture2D ground1; 
     List<Vector2> vectors = new List<Vector2>(); 


     Vector2 playerPosition = new Vector2(30, 300); 
     Vector2 ground1p1 = new Vector2(0,430); 
     Vector2 ground1p2 = new Vector2(200,430); 
     Vector2 ground1p3 = new Vector2(0, 290); 
     Vector2 ground1p4 = new Vector2(280, 340); 
+0

我不认为加入另一个循环将有助于XNA的性能。诚实合适的数据结构可以更好地处理这个问题。 – ChaosPandion

+0

我知道它不会帮助大声笑:D我希望它对我来说很容易进一步写作我不想要复制粘贴100次 –

回答

1

你可以使用一个列表(如你所说)

List<Vector2> vectors = new List<Vector2>(); 

vectors.Add(ground1p1); 
vectors.Add(ground1p2); 
vectors.Add(ground1p3); 
vectors.Add(ground1p4); 

foreach (Vector2 vec2 in vectors) 
{ 
    if (DetectPlayerAndGround1Collision2(playerPosition, vec2, player, ground1)) 
    { 
     hasJumped = false; 
     velocity.Y = 0f;   

     // maybe add a break to prevent superfluous calls 
     break; 
    } 
} 
+0

感谢这个快速和良好的答案人 –

+0

某种程度上列表 vectors = new List ();不工作 当我添加向量列表错误出现 –

+0

您可能需要'使用System.Collections.Generic'在您的文件的顶部。另请参阅http://msdn.microsoft.com/en-us/library/6sh2ey19.aspx – Matthew