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

C# 向量列表需要一些基础知识,c#,xna-4.0,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,groun

嗨,我有这些向量:

    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);
我想把它们放在一个列表中,所以不要这样做:

    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);
当我在写的末尾写“vectors”时,问题就出在这里,没有什么事情会像我没有声明的那样被列为一个列表:

    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:Microsoft.Xna.Framework.Game
{
图形管理器图形;
SpriteBatch SpriteBatch;
公共游戏1()
{
graphics=新的GraphicsDeviceManager(此);
Content.RootDirectory=“Content”;
}
受保护的覆盖无效初始化()
{
base.Initialize();
}
布尔=真;
矢量2速度;
纹理2D播放器;
纹理2d-ground1;
列表向量=新列表();
Vector2播放位置=新Vector2(30300);
矢量2地面1P1=新矢量2(0430);
矢量2地面1P2=新矢量2(200430);
矢量2 ground1p3=新矢量2(0290);
矢量2 ground1p4=新矢量2(280340);
您可以使用列表(如您所建议的)

列表向量=新列表();
矢量。添加(ground1p1);
矢量。添加(ground1p2);
矢量。添加(ground1p3);
矢量。添加(ground1p4);
foreach(向量中的向量2 vec2)
{
if(检测播放和地面1集合2(播放位置,vec2,播放,地面1))
{
hasshopped=false;
速度Y=0f;
//也许可以增加休息时间,以防止多余的电话
打破
}
}

我不认为添加另一个循环会有助于提高XNA的性能。诚实地说,适当的数据结构可以更好地处理这个问题。我知道这不会有什么帮助。lol:D我希望以后的写作对我来说很容易,我不想复制粘贴100次。不知何故,列表向量=新列表();在我向列表中添加向量时不起作用错误出现您可能需要使用文件顶部的System.Collections.Generic。另请参见列表向量=新列表();在我编写向量时。添加“向量”是一个字段,它的用法与公共类Game1:Microsoft.Xna.Framework.Game之后的类型类似{