2013-12-10 63 views
0

我试图做一个滑块的音乐,音效和分辨率,其中2个工作,但一个是坏:鼠标,绘画和滑块图像

enter image description here

我有4类,互动与图像。第一个是Images.cs类,它调用所有图像并将它们放入Content方法中。我的主课程包含Content方法,然后更新大小为Resize.cs类的图像,并将Options.cs类放在正确的位置。

ImagesResize应该不是问题,因为它们不会与图像的绘制进行交互。图像的绘制方式很重要(如果您最后绘制背景,则会覆盖其他所有图像,因此您需要先绘制图像)。

下面是代码(有些代码已经采取,因为限制了):

Game1.cs:

public class Game1 : Microsoft.Xna.Framework.Game 
    { 
     #region Define 
     GraphicsDeviceManager graphics; 
     SpriteBatch spriteBatch; 
     public static SpriteFont font1; 
     private static StartUp.Resize resize; 
     private StartUp.TitleScreen titlescreen; 
     private StartUp.Options options; 
     private StartUp.Credits credits; 

     Rectangle background = new Rectangle(0, 0, 0, 0); 

     enum GameStates { StartUp, TitleScreen, Options, Credits, Paused, Playing, Death, Continue } 
     GameStates gameState = GameStates.TitleScreen; 

     #region Mouse 
     float mouseDelay = 0.01f; 
     float mouseTime = 0.0f; 
     public static bool mouseActive = true; 
     public static bool mouseUsed = false; 
     public static string mouseOn = "None"; 
     public static Vector2 mouseLocation; 
     #endregion 

     #region Slider 
     public static int sliderMax; 
     public static int sliderMin; 
     #endregion 
     #endregion 

     #region Initialize 
     protected override void Initialize() 
     { 
      this.IsMouseVisible = true; 
      resize = new StartUp.Resize(); 
      options = new StartUp.Options(); 
      titlescreen = new StartUp.TitleScreen(); 
      StartUp.Options.Initialize(); 
      base.Initialize(); 
     } 
     #endregion 

     #region Update 
     protected override void Update(GameTime gameTime) 
     { 
      // Allows the game to exit 
      if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed) 
       this.Exit(); 

      #region SetUp 
      sliderMin = (resize.size * 69) + ((resize.size * 1)); 
      sliderMax = (resize.size * 69) + ((resize.size * 32)); 
      #endregion 

      if (this.IsMouseVisible == true) 
      { 
       #region Mouse 
       mouseTime += (float)gameTime.ElapsedGameTime.TotalSeconds; 

       MouseState mouse = Mouse.GetState(); 

       mouseLocation = new Vector2(mouse.X, mouse.Y); 

       #region GameState Switch 
       switch (gameState) 
       { 
        case GameStates.TitleScreen: 
         StartUp.TitleScreen.Update(gameTime); 
         break; 
        case GameStates.Options: 
         StartUp.Options.Update(gameTime); 
         break; 
        case GameStates.Credits: 
         break; 
       } 
       #endregion 

       if (mouseOn != "None") 
       { 
        mouseUsed = true; 

        #region GameState Switch 
        switch (gameState) 
        { 
         case GameStates.TitleScreen: 
          if (mouseOn == "Continue") 
           gameState = GameStates.Continue; 
          else if (mouseOn == "Credits") 
           gameState = GameStates.Credits; 
          else if (mouseOn == "Exit") 
           this.Exit(); 
          else if (mouseOn == "Logo") 
           gameState = GameStates.TitleScreen; 
          else if (mouseOn == "Options") 
           gameState = GameStates.Options; 
          else if (mouseOn == "Play") 
           gameState = GameStates.Playing; 
          else if (mouseOn == "Version") 
           gameState = GameStates.TitleScreen; 
          else 
           mouseOn = "None"; 
          break; 
         case GameStates.Options: 
          if (mouseOn == "Main Menu") 
           gameState = GameStates.TitleScreen; 
          if (mouseOn == "Ok") 
          { 
          } 
          else if (mouseOn == "Off") 
          { 
           graphics.IsFullScreen = false; 
           graphics.ApplyChanges(); 
          } 
          else if (mouseOn == "On") 
          { 
           graphics.IsFullScreen = true; 
           graphics.ApplyChanges(); 
          } 
          break; 
         case GameStates.Credits: 
          break; 
        } 
        #endregion 

        mouseOn = "None"; 
       } 

       if (mouse.LeftButton == ButtonState.Pressed) 
       { 
        mouseTime = 0.0f; 
        mouseActive = false; 
       } 

       if (mouse.LeftButton == ButtonState.Released && mouseTime > mouseDelay) 
       { 
        mouseActive = true; 
        mouseUsed = false; 
       } 
       #endregion 
      } 

      #region GameState Switch 
      switch (gameState) 
      { 
       case GameStates.StartUp: 
        break; 
       case GameStates.TitleScreen: 
        StartUp.TitleScreen.Update(gameTime); 
        break; 
       case GameStates.Options: 
        StartUp.Options.Update(gameTime); 
        break; 
       case GameStates.Credits: 
        break; 
      } 
      #endregion 

      #region Image Resize 
      if ((gameState == GameStates.TitleScreen || 
       gameState == GameStates.Credits || 
       gameState == GameStates.Options || 
       gameState == GameStates.Continue) && 
       (resize.change == true)) 
      { 
       resize.change = false; 
       resize.backgroundHeight = resize.size * StartUp.Images.Background.Height; 
       resize.backgroundWidth = resize.size * StartUp.Images.Background.Width; 
       resize.backgroundX = 0; 
       resize.backgroundY = 0; 
       graphics.PreferredBackBufferHeight = resize.backgroundHeight; 
       graphics.PreferredBackBufferWidth = resize.backgroundWidth; 
       graphics.ApplyChanges(); 

       background = new Rectangle(resize.backgroundX, resize.backgroundY, resize.backgroundWidth, resize.backgroundHeight); 
       //Background's rectangle is located in the draw method. 

       resize.sliderHeight = resize.size * StartUp.Images.Slider.Height; 
       resize.sliderWidth = resize.size * StartUp.Images.Slider.Width; 
       options.sliderX = resize.size * 70; 
       options.sliderY = resize.size * 38; 
       StartUp.Options.slide = new Rectangle(options.sliderX, options.sliderY, resize.sliderWidth, resize.sliderHeight); 

       resize.sliderHeight = resize.size * StartUp.Images.Slider.Height; 
       resize.sliderWidth = resize.size * StartUp.Images.Slider.Width; 
       options.slider2X = resize.size * 70; 
       options.slider2Y = resize.size * 52; 
       StartUp.Options.slide2 = new Rectangle(options.slider2X, options.slider2Y, resize.sliderWidth, resize.sliderHeight); 

       resize.sliderHeight = resize.size * StartUp.Images.Slider.Height; 
       resize.sliderWidth = resize.size * StartUp.Images.Slider.Width; 
       options.slider3X = resize.size * 70; 
       options.slider3Y = resize.size * 63; 
       StartUp.Options.slide3 = new Rectangle(options.slider3X, options.slider3Y, resize.sliderWidth, resize.sliderHeight); 

       resize.sliderbackgroundHeight = resize.size * StartUp.Images.SliderBackground.Height; 
       resize.sliderbackgroundWidth = resize.size * StartUp.Images.SliderBackground.Width; 
       options.sliderbackgroundX = resize.size * 69; 
       options.sliderbackgroundY = resize.size * 36; 
       StartUp.Options.slideback = new Rectangle(options.sliderbackgroundX, options.sliderbackgroundY, resize.sliderbackgroundWidth, resize.sliderbackgroundHeight); 

       resize.sliderbackgroundHeight = resize.size * StartUp.Images.SliderBackground.Height; 
       resize.sliderbackgroundWidth = resize.size * StartUp.Images.SliderBackground.Width; 
       options.sliderbackground2X = resize.size * 69; 
       options.sliderbackground2Y = resize.size * 50; 
       StartUp.Options.slideback2 = new Rectangle(options.sliderbackground2X, options.sliderbackground2Y, resize.sliderbackgroundWidth, resize.sliderbackgroundHeight); 

       resize.sliderbackgroundHeight = resize.size * StartUp.Images.SliderBackground.Height; 
       resize.sliderbackgroundWidth = resize.size * StartUp.Images.SliderBackground.Width; 
       options.sliderbackground3X = resize.size * 69; 
       options.sliderbackground3Y = resize.size * 64; 
       StartUp.Options.slideback3 = new Rectangle(options.sliderbackground3X, options.sliderbackground3Y, resize.sliderbackgroundWidth, resize.sliderbackgroundHeight); 
      } 
      #endregion 

      base.Update(gameTime); 
     } 
     #endregion 

     #region Draw 
     protected override void Draw(GameTime gameTime) 
     { 
      GraphicsDevice.Clear(Color.CornflowerBlue); 

      if (gameState == GameStates.TitleScreen || 
       gameState == GameStates.Options || 
       gameState == GameStates.Credits) 
      { 
       spriteBatch.Begin(SpriteSortMode.FrontToBack, BlendState.AlphaBlend, SamplerState.PointClamp, DepthStencilState.Default, RasterizerState.CullCounterClockwise); 
       spriteBatch.Draw(StartUp.Images.Background, background, Color.White); 
       spriteBatch.End(); 
      } 

      if (gameState == GameStates.TitleScreen) 
      { 
       StartUp.TitleScreen.Draw(spriteBatch); 
      } 

      if (gameState == GameStates.Options) 
      { 
       StartUp.Options.Draw(spriteBatch); 

       spriteBatch.Begin(SpriteSortMode.FrontToBack, BlendState.AlphaBlend, SamplerState.PointClamp, DepthStencilState.Default, RasterizerState.CullCounterClockwise); 
       if (graphics.IsFullScreen == true) 
       { 
        spriteBatch.Draw(StartUp.Images.OffNotSelected, StartUp.Options.offnot, Color.White); 
        spriteBatch.Draw(StartUp.Images.OnSelected, StartUp.Options.onsel, Color.White); 
       } 
       else 
       { 
        spriteBatch.Draw(StartUp.Images.OffSelected, StartUp.Options.offsel, Color.White); 
        spriteBatch.Draw(StartUp.Images.OnNotSelected, StartUp.Options.onnot, Color.White); 
       } 
       spriteBatch.End(); 
      } 

      if (Keyboard.GetState().IsKeyDown(Keys.Space)) 
      { 
       spriteBatch.Begin(); 
       spriteBatch.Draw(StartUp.Images.Transparent, new Rectangle(0, 0, 800, 300), Color.White); 
       //debug 1 
       spriteBatch.DrawString(font1, "Resize", new Vector2(0, 0), Color.White); 
       spriteBatch.DrawString(font1, StartUp.Options.res.ToString(), new Vector2(0, 20), Color.White); 
       spriteBatch.DrawString(font1, options.resX.ToString(), new Vector2(0, 40), Color.White); 
       spriteBatch.DrawString(font1, options.resY.ToString(), new Vector2(0, 60), Color.White); 
       spriteBatch.DrawString(font1, resize.resHeight.ToString(), new Vector2(0, 80), Color.White); 
       spriteBatch.DrawString(font1, resize.resWidth.ToString(), new Vector2(0, 100), Color.White); 

       //debug2 
       spriteBatch.DrawString(font1, "Slider", new Vector2(380, 0), Color.White); 
       spriteBatch.DrawString(font1, "sliderMin: " + sliderMin.ToString(), new Vector2(380, 20), Color.White); 
       spriteBatch.DrawString(font1, "sliderMax: " + sliderMax.ToString(), new Vector2(380, 40), Color.White); 
       spriteBatch.DrawString(font1, StartUp.Options.thing.ToString(), new Vector2(380, 60), Color.White); 

       //debug3 
       spriteBatch.DrawString(font1, "Background", new Vector2(0, 120), Color.White); 
       spriteBatch.DrawString(font1, background.ToString(), new Vector2(0, 140), Color.White); 
       spriteBatch.DrawString(font1, resize.backgroundHeight.ToString(), new Vector2(0, 160), Color.White); 
       spriteBatch.DrawString(font1, resize.backgroundWidth.ToString(), new Vector2(0, 180), Color.White); 
       spriteBatch.DrawString(font1, resize.backgroundX.ToString(), new Vector2(0, 200), Color.White); 
       spriteBatch.DrawString(font1, resize.backgroundY.ToString(), new Vector2(0, 220), Color.White); 
       //debug4 
       spriteBatch.DrawString(font1, "Mouse", new Vector2(380, 120), Color.White); 
       spriteBatch.DrawString(font1, "mouseDelay: " + mouseDelay.ToString(), new Vector2(380, 140), Color.White); 
       spriteBatch.DrawString(font1, "mouseTime: " + mouseTime.ToString(), new Vector2(380, 160), Color.White); 
       spriteBatch.DrawString(font1, "mouseActive: " + mouseActive.ToString(), new Vector2(380, 180), Color.White); 
       spriteBatch.DrawString(font1, "mouseUsed: " + mouseUsed.ToString(), new Vector2(380, 200), Color.White); 
       spriteBatch.DrawString(font1, "mouseOn: " + mouseOn.ToString(), new Vector2(380, 220), Color.White); 
       spriteBatch.DrawString(font1, "mouseLocation: " + mouseLocation.ToString(), new Vector2(380, 240), Color.White); 
       spriteBatch.End(); 
      } 
      base.Draw(gameTime); 
     } 
     #endregion 
    } 
} 

Options.cs:

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Text; 
using Microsoft.Xna.Framework; 
using Microsoft.Xna.Framework.Content; 
using Microsoft.Xna.Framework.Graphics; 
using Microsoft.Xna.Framework.Input; 

namespace **.StartUp 
{ 
    class Options 
    { 
     #region Define 
     private static Resize resize; 

     public static bool thing; 

     public int resX; 
     public int resY; 

     public int optionslogoX; 
     public int optionslogoY; 

     public int offnotselectedX; 
     public int offnotselectedY; 

     public int offselectedX; 
     public int offselectedY; 

     public int okX; 
     public int okY; 

     public int onnotselectedX; 
     public int onnotselectedY; 

     public int onselectedX; 
     public int onselectedY; 

     public int fullscreenX; 
     public int fullscreenY; 

     public int menuX; 
     public int menuY; 

     public int musicX; 
     public int musicY; 

     public int resolutionX; 
     public int resolutionY; 

     public int sliderbackgroundX; 
     public int sliderbackgroundY; 

     public int sliderbackground2X; 
     public int sliderbackground2Y; 

     public int sliderbackground3X; 
     public int sliderbackground3Y; 

     public int sliderX; 
     public int sliderY; 

     public int slider2X; 
     public int slider2Y; 

     public int slider3X; 
     public int slider3Y; 

     public int soundfxX; 
     public int soundfxY; 

     public static Rectangle full; 
     public static Rectangle menu; 
     public static Rectangle music; 
     public static Rectangle slideback; 
     public static Rectangle slideback2; 
     public static Rectangle slideback3; 
     public static Rectangle ok; 
     public static Rectangle slide; 
     public static Rectangle slide2; 
     public static Rectangle slide3; 
     public static Rectangle sound; 
     public static Rectangle optionslogo; 
     public static Rectangle offnot; 
     public static Rectangle offsel; 
     public static Rectangle onnot; 
     public static Rectangle onsel; 
     public static Rectangle resolution; 
     public static Rectangle res; 
     #endregion 

     #region Update and Draw 
     public static void Update(GameTime gameTime) 
     { 
      #region Mouse 
      MouseState mouse = Mouse.GetState(); 

      if (mouse.LeftButton == ButtonState.Pressed && Game1.mouseUsed == false && Game1.mouseActive == true) 
      { 
       if (menu.Contains(mouse.X, mouse.Y)) 
       { 
        Game1.mouseOn = "Main Menu"; 
        Game1.mouseUsed = true; 
       } 
       else if (ok.Contains(mouse.X, mouse.Y)) 
       { 
        Game1.mouseOn = "Ok"; 
        Game1.mouseUsed = true; 
       } 
       else if (offnot.Contains(mouse.X, mouse.Y)) 
       { 
        Game1.mouseOn = "Off"; 
        Game1.mouseUsed = true; 
       } 
       else if (onnot.Contains(mouse.X, mouse.Y)) 
       { 
        Game1.mouseOn = "On"; 
        Game1.mouseUsed = true; 
       } 
       else if (slide.Contains(mouse.X, mouse.Y)) 
       { 
        Game1.mouseOn = "Slide"; 
        Game1.mouseUsed = true; 
       } 
       else if (slide2.Contains(mouse.X, mouse.Y)) 
       { 
        Game1.mouseOn = "Slide2"; 
        Game1.mouseUsed = true; 
       } 
       else if (slide3.Contains(mouse.X, mouse.Y)) 
       { 
        Game1.mouseOn = "Slide3"; 
        Game1.mouseUsed = true; 
       } 
      } 
      #endregion 

      #region Slider 
      if (mouse.X >= Game1.sliderMin && mouse.X <= Game1.sliderMax) 
       thing = true; 
      else 
       thing = false; 

      if (mouse.LeftButton == ButtonState.Pressed && slideback.Contains(mouse.X, mouse.Y) && (mouse.X >= (Game1.sliderMin + (1 * resize.size))) && (mouse.X <= Game1.sliderMax)) 
      { 
       slide.X = mouse.X - 1 * resize.size; 
      } 
      if (mouse.LeftButton == ButtonState.Pressed && slideback2.Contains(mouse.X, mouse.Y) && (mouse.X >= (Game1.sliderMin + (1 * resize.size))) && (mouse.X <= Game1.sliderMax)) 
      { 
       slide2.X = mouse.X - 1 * resize.size; 
      } 
      if (mouse.LeftButton == ButtonState.Pressed && slideback3.Contains(mouse.X, mouse.Y) && (mouse.X >= (Game1.sliderMin + (1 * resize.size))) && (mouse.X <= Game1.sliderMax)) 
      { 
       slide3.X = mouse.X - 1 * resize.size; 
      } 
      #endregion 
     } 

     public static void Draw(SpriteBatch spriteBatch) 
     { 
      spriteBatch.Begin(SpriteSortMode.FrontToBack, BlendState.AlphaBlend, SamplerState.PointClamp, DepthStencilState.Default, RasterizerState.CullCounterClockwise); 
      spriteBatch.Draw(Images.Res1, res, Color.White); 
      spriteBatch.Draw(Images.Res2, res, Color.White); 
      spriteBatch.Draw(Images.Res3, res, Color.White); 
      spriteBatch.Draw(Images.Res4, res, Color.White); 
      spriteBatch.Draw(Images.Res5, res, Color.White); 
      spriteBatch.Draw(Images.Res6, res, Color.White); 
      spriteBatch.Draw(Images.Res7, res, Color.White); 
      spriteBatch.Draw(Images.Res8, res, Color.White); 
      spriteBatch.Draw(Images.Ok, ok, Color.White); 
      spriteBatch.Draw(Images.OptionsLogo, optionslogo, Color.White); 
      spriteBatch.Draw(Images.FullScreen, full, Color.White); 
      spriteBatch.Draw(Images.Menu, menu, Color.White); 
      spriteBatch.Draw(Images.Music, music, Color.White); 
      spriteBatch.Draw(Images.Resolution, resolution, Color.White); 
      spriteBatch.Draw(Images.SliderBackground, slideback, Color.White); 
      spriteBatch.Draw(Images.SliderBackground, slideback2, Color.White); 
      spriteBatch.Draw(Images.SliderBackground, slideback3, Color.White); 
      spriteBatch.Draw(Images.Slider, slide, Color.White); 
      spriteBatch.Draw(Images.Slider, slide2, Color.White); 
      spriteBatch.Draw(Images.Slider, slide3, Color.White); 
      spriteBatch.Draw(Images.SoundFX, sound, Color.White); 
      spriteBatch.End(); 
     } 
     #endregion 

     #region Method 
     #endregion 

     #region In 
     public static void Initialize() 
     { 
      resize = new Resize(); 
     } 
     #endregion 
    } 
} 
+0

请尽量避免在一个问题中发布所有的代码,这是令人沮丧的阅读。你应该只发布相关的代码,这样更容易理解。 – pinckerman

+0

只是一个建议,你可以在它们之间用','声明多个变量。我的意思是'int var1,var2,var3 ...;'那件事真的很糟糕。 – pinckerman

+0

@pinckerman感谢您的煽动,我将在未来发布时记住这一点。我只完全放下代码是因为我不知道它是否在'Game1.cs'中的int或其他代码部分。感谢您的帮助,并会尝试下面发布的代码。 –

回答

0

您正在使用

spriteBatch.Begin(SpriteSortMode.FrontToBack, ...); 

但你一个请不要设置SpriteBatch.Draw方法的layerDepth参数。

如果您希望每个精灵都按您调用Draw的顺序绘制,则应该使用SpriteSortMode.Deferred

否则,如果你想使用FrontToBack你需要使用不同的过载SpriteBatch.Draw,例如:

public void Draw (
    Texture2D texture, 
    Vector2 position, 
    Nullable<Rectangle> sourceRectangle, 
    Color color, 
    float rotation, 
    Vector2 origin, 
    float scale, 
    SpriteEffects effects, 
    float layerDepth 
) 

这样你可以设置layerDepth参数指定每个精灵的深度。
广告MSDN说:

层的深度。默认情况下,0表示前层,1 表示后层。如果您希望精灵在绘图过程中排序为 ,请使用SpriteSortMode。

参考:Draw方法和SpriteSortMode枚举。

+0

感谢您的帮助,我很抱歉代码混乱且难以阅读,我正在尽全力作为编码的第一年来整理和保持代码整洁有序的方式。 –

+0

别担心,我们都在这里学习:) – pinckerman