XNA 2D基本碰撞列表



我是编程世界中的初学者,现在我正在尝试C#中的XNA编程,现在我正在从事一款基本游戏,其中玩家是飞机或太空飞船,您必须击落流星。

目前我正在尝试盒子碰撞检测,我无法完全弄清楚,我不知道如何获得列表中每个项目的位置,我需要您的帮助!

因此,我基本上有一个造船和庆祝延迟。模型文本绘制了10次,并具有随机位置。我想在每个流星周围制作矩形,但我不知道如何。我尝试了下面显示的foreach环路,但在与之碰撞时只有一个流星起作用。抱歉,如果我的英语不是最好的,我感谢大家的帮助!

List<Vector2> meteor_pos = new List<Vector2>();
              //loadcontent
            for (int i = 0; i < 10; i++)
            {
                meteor_pos.Add(new Vector2(myRnd.Next(800), myRnd.Next(600)));
                double tmp_angle = (myRnd.Next(1000) * Math.PI * 2) / 1000.0;
                double tmp_speed = 0.5 + 3.0 * (myRnd.Next(1000) / 1000.0);
                meteor_speed.Add(new Vector2((float)(tmp_speed * Math.Cos(tmp_angle)),
                (float)(tmp_speed * Math.Sin(tmp_angle))));
            }
              //protected override void Update(GameTime gameTime)
              for (int i = 0; i < meteor_pos.Count; i++)
                {
                    meteor_pos[i] += meteor_speed[i];
                    Vector2 v = meteor_pos[i];
                    //Outside the screen?
                    if (v.X < -80)
                        v.X = graphics.GraphicsDevice.Viewport.Width + 80;
                    if (v.X > graphics.GraphicsDevice.Viewport.Width + 80)
                        v.X = -80;
                    if (v.Y < -60)
                        v.Y = graphics.GraphicsDevice.Viewport.Height + 60;
                    if (v.Y > graphics.GraphicsDevice.Viewport.Height + 60)
                        v.Y = -60;
                    //Uppdate the list
                    meteor_pos[i] = v;
               }
                foreach (var item in meteor_pos)      
                 {
                     meteor_rect = new Rectangle((int)item.X, (int)item.Y, gfx_meteor.Width, gfx_meteor.Height);
                 }
                gfx_rect = new Rectangle((int)position.X, (int)position.Y, gfx.Width, gfx.Height);
                if (gfx_rect.Intersects(meteor_rect))
                {
                    position.X = 0;
                    position.Y = 0;
                }
    //protected override void Draw(GameTime gameTime)
                for (int i = 0; i < meteor_pos.Count; i++)
            {
                spriteBatch.Draw(gfx_meteor, meteor_pos[i], null, Color.White, 0,
                new Vector2(gfx_meteor.Width / 2, gfx_meteor.Height / 2), 1.0f, SpriteEffects.None, 0);
            }}

您的方法非常令人困惑,您将要创建一个类,至少可能是一个可以处理位置和其他基本信息的精灵类。

但是,要解决您的当前问题,您正在覆盖foreach循环中的Meteor_Rect的值,然后在最后一次检查碰撞。

因此,将代码切换为这样:

gfx_rect = new Rectangle((int)position.X, (int)position.Y, gfx.Width, gfx.Height);
foreach (var item in meteor_pos)      
    {
        meteor_rect = new Rectangle((int)item.X, (int)item.Y, gfx_meteor.Width, gfx_meteor.Height);
        if (gfx_rect.Intersects(meteor_rect))
            {
                position.X = 0;
                position.Y = 0;
            }
    }

,但就像我说的那样,请查找一个基本教程,以帮助您:)

最新更新