2013-04-01 40 views
0

我遇到了麻煩,使得SplashScreen切換到播放狀態。我宣佈遊戲狀態的枚舉在Game1.csScreenState故障XNA 4 C#

public enum GameState { SplashScreen, Playing } 
public GameState currentState; 

然後,我有這個在Game1.cs

protected override void LoadContent() 
{ 
    // Create a new SpriteBatch, which can be used to draw textures. 
    spriteBatch = new SpriteBatch(GraphicsDevice); 

    splashScreen.LoadContent(Content); 
    playState.LoadContent(Content); 
} 

的LoadContent在更新中Game1.cs

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

    switch (currentState) 
    { 
     case GameState.SplashScreen: 
     { 
      splashScreen.Update(gameTime); 
      break; 
     } 
     case GameState.Playing: 
     { 
      playState.Update(gameTime); 
      break; 
     } 
    } 

    base.Update(gameTime); 
} 

最後在Game1.cs中我有我的畫

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

    spriteBatch.Begin(); 

    switch (currentState) 
    { 
     case GameState.SplashScreen: 
     { 
      splashScreen.Draw(spriteBatch); 
      break; 
     } 
     case GameState.Playing: 
     { 
      playState.Draw(spriteBatch) ; 
      break; 
     } 
    } 

    spriteBatch.End(); 

    base.Draw(gameTime); 
} 

這裏是我的SplashScreen.cs

public class SplashScreen 
{ 
    Texture2D tSplash; 
    Vector2 vSplash = Vector2.Zero; 

    Game1 main = new Game1(); 

    public void LoadContent(ContentManager Content) 
    { 
     tSplash = Content.Load<Texture2D>("splash"); 
    } 

    public void Update(GameTime gameTime) 
    { 
     if (Keyboard.GetState().IsKeyDown(Keys.X)) 
     { 
      main.currentState = Game1.GameState.Playing; 
     } 

    } 

    public void Draw(SpriteBatch spriteBatch) 
    { 
     spriteBatch.Draw(tSplash, vSplash, Color.White); 
    } 
} 

而且我Playing.cs

class Playing 
{ 
    Texture2D tBack; 
    Vector2 vBack = Vector2.Zero; 

    Game1 mainG = new Game1(); 

    public void Initialize() 
    { 
    } 

    public void LoadContent(ContentManager contentManager) 
    { 
     tBack = contentManager.Load<Texture2D>("playing"); 
    } 

    public void Update(GameTime gameTime) 
    { 

    } 

    public void Draw(SpriteBatch spriteBatch) 
    { 
     spriteBatch.Draw(tBack, vBack, Color.White); 
    } 
} 

的閃屏圖像顯示出來,但是當我按X不發生PlatyingState。

回答

0

您正在爲每個屏幕聲明一個全新的Game對象。永遠不要這樣做。一個Game是爲了代表你的整個遊戲。

但是,可以將多個引用爲一個單一的Game對象。

保留一個單一的Game對象在最頂層;你已經擁有了屏幕對象。你甚至可以保持mainmainG變量在你的屏幕,但讓他們,而不是指向你的單Game

public class SplashScreen 
{ 
    Game1 main; 
    public SplashScreen(Game1 g) 
    { 
     main = g; 
    } 

    //rest can be largely the same 
} 

基本上做同樣的播放(需要在Game1,並設定參考,而不是重新聲明) 。

然後在Game1.cs:

protected override void LoadContent() 
{ 
    // Create a new SpriteBatch, which can be used to draw textures. 
    spriteBatch = new SpriteBatch(GraphicsDevice); 

    splashScreen = new SplashScreen(this); 
    splashScreen.LoadContent(Content); 
    playState = new Playing(this); 
    playState.LoadContent(Content); 
} 

這將是一個良好的開端,但我建議採取這一更進一步,創造了所有螢幕的基類。查看official XNA sample以獲得遊戲屏幕管理。

+0

感謝您的幫助! – freemann098