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