是否可以在XNA游戏中打开自定义屏幕或弹出窗口(模态)。想法是向用户显示游戏的一些选项以及将成为应用程序套件一部分的其他应用程序。
public class MyGame : Game
{
#region Initialization
/// <summary>
/// Creates a new instance of the game.
/// </summary>
public MyGame()
{
graphics = new GraphicsDeviceManager(this);
Content.RootDirectory = "Content";
///etc;
}
/// <summary>
/// Performs initializations required by the game.
/// </summary>
protected override void Initialize()
{
///I would Like to open a new screen before the actual game
/// menu is displayed ... also return to this screen after the logic is run
/// in that screen
base.Initialize();
}
}
任何想法/建议/样品。 感谢Adv。
KRZ。
谢谢Blau.I尝试了DrawableGameComponent解决方案(复制了文章中的代码)但是徒劳无功。无论我做什么,只显示一个空白屏幕。
在游戏构造函数中,我放了一个条件并将方法调用为
if (showScreen)
{
LaunchScreen launcher = new LaunchScreen(this, null);
//Components.Add(launcher);
launcher.Show();
}
else{///constructor code}
我还在Game Initialize中添加了一个条件,这样如果showScreen是flase然后执行。(不确定它是否是一个正确的方法)
班级看起来像这样:
public class LaunchScreen : DrawableGameComponent
{
private Texture2D backgroundImage = null;
private SpriteBatch spriteBatch = null;
public LaunchScreen(Game game, Texture2D texture)
: base(game)
{
backgroundImage = texture;
Visible = false;
Enabled = false;
}
public virtual void Show()
{
Visible = true;
Enabled = true;
}
public virtual void Hide()
{
Visible = false;
Enabled = false;
}
public override void Initialize()
{
base.Initialize();
}
public override void Update(GameTime gameTime)
{
base.Update(gameTime);
}
public override void Draw(GameTime gameTime)
{
//if (backgroundImage != null)
//{
spriteBatch = new SpriteBatch(base.GraphicsDevice);
if (Game.Services.GetService(typeof(SpriteBatch)) == null)
Game.Services.AddService(typeof(SpriteBatch), spriteBatch);
Rectangle screen = new Rectangle(0, 0,
Game.Window.ClientBounds.Width,
Game.Window.ClientBounds.Height);
Texture2D lauT2D = this.Game.Content.Load<Texture2D>("Textures/BG");
spriteBatch.Draw(lauT2D, screen, Color.White);
//}
///SpriteFont font = this.Game.Content.Load<SpriteFont>("Fonts/MenuFont");
///Vector2 spritePosition = Vector2.One;
///spriteBatch.DrawString(font, "Goodness ME", spritePosition, Color.White);
base.Draw(gameTime);
}
}
我对如何初始化GraphicDevice和SpriteBatch以及在实际游戏运行/初始化之前打开窗口感到困惑。 KRZ
答案 0 :(得分:2)
这是一叠屏幕,顶部屏幕是更新/绘制的屏幕
您可以为菜单,配置,教程,独奏游戏,多人游戏,暂停菜单等实现屏幕。
public class ScreenManager : DrawableGameComponent
{
Stack<Screen> Screens = new Stack<Screen>();
public event Action<Screen> PopDone;
public event Action<Screen> PushDone;
public override void Update(gametime)
{
if (Screens.Count>0) Screens.Peek().Update(gametime);
}
public override void Draw(gametime)
{
if (Screens.Count>0) Screens.Peek().Draw(gametime);
}
public void Push(Screen screen)
{
Stack.Push(screen);
if (PushDone!=null) PushDone(screen);
}
public void Pop()
{
Screen screen = Stack.Pop();
if (PopDone!=null) PopDone(screen);
}
}
public abstract class Screen : DrawableGameComponent()
{
protected ScreenManager Manager;
public Screen(ScreenManager manager) {
this.Manager = manager;
}
// Your template code for screens
}
public class MenuScreen : Screen
{
public void Update()
{
if (ChooseConfig())
{
Manager.Push(new ConfigScreen());
}
}
}
public class ConfigScreen : Screen
{
public void Update()
{
if (ChooseReturn())
{
Manager.Pop();
}
}
}
public Game1: Game {
ScreenManager Manager;
public override Initialize()
{
Manager = new ScreenManager(this);
Manager.PopDone += ScreenPopDone;
Manager.Push( new MenuScreen() );
}
void ScreenPopDone(Screen sender)
{
if (sender is MenuScreen)
{
Exit();
}
}
}