【问题标题】:Choosing a random value from an enum and then using switch to draw textures XNA c#从枚举中选择一个随机值,然后使用 switch 绘制纹理 XNA c#
【发布时间】:2012-07-25 07:01:37
【问题描述】:
    enum rocks
    {
        uno = 1,
        dos,
        tres 

    }

所以我在顶部声明我的枚举,

rocks[] values = {rocks.uno, rocks.dos, rocks.tres};
        Random random = new Random();
        rocks randomValue = values[random.Next(values.Length)];

初始化我的随机数生成器

private void drawUno(SpriteBatch spriteBatch)
    {
        spriteBatch.Draw(rock1, Vector2.Zero, Color.White);
    }

有不同的方法来绘制每块石头

 public void drawRocks(SpriteBatch spriteBatch)
    {
        switch (Rocks)
        {
            case rocks.uno:
                drawUno(spriteBatch);
                break;
            case rocks.dos:
                drawDos(spriteBatch);
                break;
            case rocks.tres:
                drawTres(spriteBatch);
                break;
            default:
                break;
        }
    }

以及一种提取随机生成的任何岩石的方法

我在主绘制方法中回调了 drawRock,但没有任何反应,我做错了什么?

这是我的全班:

namespace PlanetDrill2
{
   public class UMA : Screen
    {


        background bgLayer1;
        background bgLayer2;
        background bgLayer3;


        Texture2D rock1;
        Texture2D rock2;
        Texture2D rock3;

        enum rocks
        {
            uno = 1,
            dos,
            tres 

        }


       rocks Rocks;


        public UMA(Game game, SpriteBatch batch, ChangeScreen changeScreen)
            : base(game, batch, changeScreen)
        {
            bgLayer1 = new background();
            bgLayer2 = new background();
            bgLayer3 = new background();

            player = new Player();

            Animation playerAnimation = new Animation();
            Texture2D playerTexture = content.Load<Texture2D>("shipAnim");
            playerAnimation.Initialize(playerTexture, Vector2.Zero, 80, 160, 5, 30, Color.White, 1f, true);

            Vector2 playerPosition = new Vector2(game.GraphicsDevice.Viewport.TitleSafeArea.X, game.GraphicsDevice.Viewport.TitleSafeArea.Y
            + game.GraphicsDevice.Viewport.TitleSafeArea.Height / 2);
            player.Initialize(playerAnimation, playerPosition);

            rock1 = content.Load<Texture2D>("rocktexture1");
            rock2 = content.Load<Texture2D>("Hardrock");
            rock3 = content.Load<Texture2D>("rarerock");

            bgLayer1.Initialize(content, "scrollingrocks", game.GraphicsDevice.Viewport.Height, -10);
            bgLayer2.Initialize(content, "scrollingrocks2", game.GraphicsDevice.Viewport.Height, -6);
            bgLayer3.Initialize(content, "scrollingrocks3", game.GraphicsDevice.Viewport.Height, -4);

            float playerMoveSpeed;


            playerMoveSpeed = 8.0f;


            rocks[] values = {rocks.uno, rocks.dos, rocks.tres};
            Random random = new Random();
            rocks randomValue = values[random.Next(values.Length)];


        }





        private void drawUno(SpriteBatch spriteBatch)
        {
            spriteBatch.Draw(rock1, Vector2.Zero, Color.White);
        }

        private void drawDos(SpriteBatch spriteBatch)
        {
            spriteBatch.Draw(rock2, Vector2.Zero, Color.White);
        }

        private void drawTres(SpriteBatch spriteBatch)
        {
            spriteBatch.Draw(rock3, Vector2.Zero, Color.White);
        }



        public void drawRocks(SpriteBatch spriteBatch)
        {

            switch (Rocks)
            {
                case rocks.uno:
                    drawUno(spriteBatch);
                    break;
                case rocks.dos:
                    drawDos(spriteBatch);
                    break;
                case rocks.tres:
                    drawTres(spriteBatch);
                    break;
                default:
                    break;
            }
        }




        private void UpdatePlayer(GameTime gameTime)
        {
            player.Update(gameTime);

            TouchPanel.EnabledGestures = GestureType.FreeDrag;

            float positionChange = 0;
            float oldPosition = 0;
            while (TouchPanel.IsGestureAvailable)
            {
                GestureSample gesture = TouchPanel.ReadGesture();

                if (gesture.GestureType == GestureType.FreeDrag)
                {
                    float newposition = gesture.Position.X;
                    if (oldPosition != 0)
                    {


                        positionChange = newposition - oldPosition;
                        player.Position.X += positionChange;
                    }
                    oldPosition = newposition;

                }

            }



            // Make sure that the player does not go out of bounds
            player.Position.X = MathHelper.Clamp(player.Position.X, 0, game.GraphicsDevice.Viewport.Width - player.Width);
            player.Position.Y = MathHelper.Clamp(player.Position.Y, 0, game.GraphicsDevice.Viewport.Height - player.Height);



        }



        protected override void SetupInputs()
        {
            base.SetupInputs();
        }

        public override void Activate()
        {
            base.Activate();
        }

        protected override void LoadScreenContent(ContentManager content)
        {
            base.LoadScreenContent(content);
        }

        protected override void UpdateScreen(GameTime gameTime, DisplayOrientation screenOrientation)
        {
            bgLayer1.Update();
            bgLayer2.Update();
            bgLayer3.Update();
            UpdatePlayer(gameTime);

            base.UpdateScreen(gameTime, screenOrientation);
        }

        protected override void DrawScreen(SpriteBatch batch, DisplayOrientation screenOrientation)
        {
            bgLayer3.Draw(batch);
            bgLayer2.Draw(batch);
            bgLayer1.Draw(batch);

            player.Draw(batch);
            drawRocks(batch);

            base.DrawScreen(batch, screenOrientation);
        }



    }
}

【问题讨论】:

  • rock r = (rock)Enum.ToObject(typeof(rock),randomizer.Next(0,Enum.GetValues(typeof(rock)).Length));

标签: c# windows-phone-7 random enums xna


【解决方案1】:

我想你忘记了什么。你正在随机挑选一块石头:

rocks randomValue = values[random.Next(values.Length)];

那么你根本不使用 randomValue 。相反,您在 switch 语句中使用Rocks

switch (Rocks)
{
    case rocks.uno:
        drawUno(spriteBatch);
        break;
    case rocks.dos:
        drawDos(spriteBatch);
        break;
    case rocks.tres:
        drawTres(spriteBatch);
        break;
    default:
        break;
}

所以要么在你的开关中使用randomValue,要么将随机值分配给你的Rocks属性。

【讨论】:

  • 我以为我在做的是通过将它们放入随机发生器来确定开关? :
  • 'rocks[] values = {rocks.uno,rocks.dos,rocks.tres};' ?
  • 是的。然后你从values 中挑选一块石头。然后你将那块石头存储在randomValue 中。那你就不用randomValue做任何事了。
  • 只需将rocks randomValue = values[random.Next(values.Length)]; 替换为Rocks = values[random.Next(values.Length)]; 即可。
  • 这确实有效,谢谢老兄,所以我猜这段代码所做的只是选择一个随机的石头,它永远不会改变?我如何让它继续产生更多?我是否应该创建另一种方法来处理当前岩石所发生的情况,并创建另一种方法来继续生成更多?
【解决方案2】:

您没有对 SpriteBatch 执行任何 Begin 和 End 调用。这些是在屏幕上显示的东西所必需的。查看文档here。它清楚地提到了这一点。

【讨论】:

  • 对不起,我需要进一步解释,spritebatch begin 和 end 调用正在另一个类中调用,这只是其中一个屏幕。
猜你喜欢
  • 2013-10-20
  • 1970-01-01
  • 2016-05-18
  • 1970-01-01
  • 2018-07-07
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多