我正在尝试制作音乐,音效和分辨率的滑块,其中2个有效,但其中一个很糟糕:
我有4个与图像交互的类。第一个是Images.cs
类,它调用所有图像并将它们放在Content
方法中。我的主要类包含Content
方法,然后通过Resize.cs
类更新大小的图像,并将Options.cs
类放在正确的位置。
Images
和Resize
应该不是问题,因为它们不会与图像的绘制相互作用。绘制图像的方式很重要(如果最后绘制背景,它将覆盖所有其他图像,因此您需要先绘制它。)
以下是代码(由于限制而取出的一些代码):
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 :(得分:0)
您正在使用
spriteBatch.Begin(SpriteSortMode.FrontToBack, ...);
但您没有设置layerDepth
方法的SpriteBatch.Draw
参数。
如果您希望按照调用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枚举。