2013-12-10 4 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 선서에 감사드립니다. 앞으로 게시 할 때 명심하십시오. 나는 단지 그것이 코드의 int 나 다른 부분에 있는지 알지 못하기 때문에 코드를 완전히 내려 놓았다. 도움을 주셔서 감사 드리며 아래에 게시 한 코드를 사용해 보시기 바랍니다. –

답변

0

당신은

를 사용하고 여기에

코드 (때문에 한계 밖으로 촬영 한 같은 일부 코드)입니다
spriteBatch.Begin(SpriteSortMode.FrontToBack, ...); 

SpriteBatch.Draw 메서드의 layerDepth 매개 변수를 설정하지 마십시오.

그리기 호출 순서대로 모든 스프라이트를 그리려면 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

관련 문제