XNA绘制和创建对象数组

时间:2014-11-14 18:59:09

标签: c# arrays xna draw

大家好我想创建一个游戏,现在,我有一个气球类,我想通过一个数组创建它们的数量,然后然后绘制到游戏中。

目前,它似乎只是创建一个气球实例或仅绘制1个实例。我多次查看它,根据我的理解,它应该循环遍历整个数组大小(目前设置为10)并创建那么多气球,然后更新和绘制那么多。

你不能做我目前正在做的数组吗?我是否必须创建每个对象?

以下是有关两个类的代码。

GameLevel: 创建onjects并绘制它们的类。

        public partial class GameLevel : GameScreen
        {
            SpriteBatch spriteBatch;
            protected Game game;

            Texture2D balloonTexture;

            Balloon[] balloons = new Balloon[10];

            public GameLevel(Game game, SpriteBatch spriteBatch)
                : base(game, spriteBatch)
            {
                this.game = game;
                this.spriteBatch = spriteBatch;
            }

            public void LoadContent()
            {

                for (int i = 0; i < balloons.Length; i++)
                {



                    int colour;
                    Random random = new Random();


                    colour = random.Next(1, 6);

                    switch (colour)
                    {
                        case 1: balloonTexture = Game.Content.Load<Texture2D>("Images/BlueBalloon");
                            break;
                        case 2: balloonTexture = Game.Content.Load<Texture2D>("Images/RedBalloon");
                            break;
                        case 3: balloonTexture = Game.Content.Load<Texture2D>("Images/YellowBalloon");
                            break;
                        case 4: balloonTexture = Game.Content.Load<Texture2D>("Images/GreenBalloon");
                            break;
                        case 5: balloonTexture = Game.Content.Load<Texture2D>("Images/PurpleBalloon");
                            break;
                    }
                    balloons[i] = new Balloon(new Rectangle(0, 0, 1152, 648), balloonTexture);
                    balloons[i].SetStartPosition();
                }
            }

            public void Update()
            {

                for (int i = 0; i < balloons.Length; i++)
                {
                    balloons[i].Update();
                }


            }



            public override void Draw(GameTime gameTime)
            {
                for (int i = 0; i < balloons.Length; i++)
                {

                    balloons[i].Draw(spriteBatch);
                }

                base.Draw(gameTime);
            }
        }
    }

Ballon Class:

    public class Balloon
    {

        Vector2 position;
        Vector2 motion;   
        Rectangle bounds;
        Rectangle screenBounds;
        public Texture2D texture;
        float balloonSpeed = 4;

        public Balloon(Rectangle screenBounds, Texture2D texture)     
        {       
            this.texture = texture;
            this.screenBounds = screenBounds;
        }

        public Rectangle Bounds
        {
            get
            {
                bounds.X = (int)position.X;
                bounds.Y = (int)position.Y;
                return bounds;
            }
        }


        public void Update()
        {
            position += motion * balloonSpeed;
        }

        private void CheckWallColision()
        {
            if (position.X < 0)
            {
                position.X = 0;
                motion.X *= -1;
            }

            if (position.X + texture.Width > screenBounds.Width)
            {
                position.X = screenBounds.Width - texture.Width;
                motion.X *= -1;
            }

            if (position.Y < 0)
            {
                position.Y = 0;
                motion.Y *= -1;
            }

            if (position.Y + texture.Height > screenBounds.Height)
            {

                position.Y = screenBounds.Height - texture.Height;
                motion.Y *= -1;
            }
        }

        public void SetStartPosition()
        {
            Random rand = new Random();

            motion = new Vector2(rand.Next(2, 6), -rand.Next(2, 6));
            motion.Normalize();

            position = new Vector2(rand.Next(100, 500), rand.Next(100, 500));

        }


        public void Draw(SpriteBatch spriteBatch)
        {
            spriteBatch.Draw(texture, position, Color.White);

        }
    }
}

1 个答案:

答案 0 :(得分:3)

我认为发生这种情况的原因与Random对象的工作方式有关。构造一个时,它将使用系统时钟作为其种子(从中生成所有值)。这意味着,如果您同时生成大量Random个实例,则每个实例中的每一个都将返回相同的值系列。如果您将这些值用于位置,则每个对象都将具有相同的位置,并且将相互绘制。在你的情况下,因为它们的大小相同,看起来只有一个被绘制过。

要解决此问题,请创建Random的单个实例,并在需要新职位时在该单个实例上使用Next()

就我个人而言,我认为这是一个非常重要的设计缺陷 - 同时创建的一系列Random个对象将随机地变得非常直观。在完全相同的方式。但是:这是一个很好的例子,&#34;随机&#34;由C#和任何其他语言创建的值实际上都是伪随机,当涉及到伪随机性时,实现非常重要。