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

私はあなたが何かを忘れたと思います。あなたはランダムな岩を選んでいます:

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 に答える
0

SpriteBatch で Begin および End 呼び出しを行っていません。これらは、画面に表示するために必要です。こちらのドキュメントをご覧ください。このことを明確に述べています。

于 2012-07-25T07:07:15.677 に答える