0
    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);
        }



    }
}
4

2 回答 2

0

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

于 2012-07-25T07:07:15.677 回答
0

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

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

然后你根本不使用 randomValue 。相反,您Rocks在 switch 语句中使用:

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

因此,要么randomValue在您的开关中使用,要么将随机值分配给您的Rocks属性。

于 2012-07-25T07:25:11.817 回答