2011-11-30 57 views
0

是否可以在XNA遊戲中打開自定義屏幕或彈出窗口(模式)。該想法是向用戶顯示一些選項wrt遊戲和其他應用程序將成爲應用程序套件的一部分。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解決方案(從文章中複製的代碼),但在vain.No無論我做什麼,僅僅顯示一個空白屏幕。

在遊戲中構造我把情況和調用方法

if (showScreen) 
    { 
     LaunchScreen launcher = new LaunchScreen(this, null); 
     //Components.Add(launcher); 
     launcher.Show(); 
    } 
else{///constructor code} 

我也提出一個條件在遊戲初始化,這樣如果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

回答

2

這是屏幕的堆棧,頂部屏幕是要更新的一個/ drawed

您可以爲您實現屏幕菜單,配置,教程,獨奏遊戲,多人遊戲,暫停菜單.. ..等等。

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

} 
+0

我在尋找加載DrawableGameComponent或者用於創建UI屏幕的東西。另外,我正在使用VS 2010 Express for Windows Phone;是否可以包含Windows窗體模板。 – srcKode

+0

我已經添加了一個DrawableGameComponent的源代碼,該代碼應該做你想做的事......你只需要啓用/禁用它來顯示/隱藏表單。 – Blau

+0

我認爲在啓動遊戲之前彈出一個窗口意味着一個新的窗口窗體......我想你只需要一個屏幕管理器系統......你想讓你的屏幕或彈出窗口成爲你遊戲的一部分嗎? – Blau