2013-04-01 38 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