Note that there are some explanatory texts on larger screens.

plurals
  1. POChoosing a random value from an enum and then using switch to draw textures XNA c#
    primarykey
    data
    text
    <pre><code> enum rocks { uno = 1, dos, tres } </code></pre> <p>So i declare my enum at the top,</p> <pre><code>rocks[] values = {rocks.uno, rocks.dos, rocks.tres}; Random random = new Random(); rocks randomValue = values[random.Next(values.Length)]; </code></pre> <p>Initialise my random number generator</p> <pre><code>private void drawUno(SpriteBatch spriteBatch) { spriteBatch.Draw(rock1, Vector2.Zero, Color.White); } </code></pre> <p>Have seperate methods to draw each rock</p> <pre><code> 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; } } </code></pre> <p>and a method to draw out whatever rock the random generates</p> <p>I call back the drawRock in my main draw method but nothing comes up, what am i doing wrong?</p> <p>Heres my whole class:</p> <pre><code>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&lt;Texture2D&gt;("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&lt;Texture2D&gt;("rocktexture1"); rock2 = content.Load&lt;Texture2D&gt;("Hardrock"); rock3 = content.Load&lt;Texture2D&gt;("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); } } } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload