向量列表需要一些基础知识

时间:2012-12-03 18:07:32

标签: c# xna-4.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);

1 个答案:

答案 0 :(得分: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;
    }
}