屏幕状态故障XNA 4 C#
我在飞溅屏幕切换到播放状态时遇到问题。我在Game1.cs中声明了游戏状态的枚举屏幕状态故障XNA 4 C#,c#,xna,xna-4.0,C#,Xna,Xna 4.0,我在飞溅屏幕切换到播放状态时遇到问题。我在Game1.cs中声明了游戏状态的枚举 public enum GameState { SplashScreen, Playing } public GameState currentState; protected override void LoadContent() { // Create a new SpriteBatch, which can be used to draw textures. spriteBatch = ne
public enum GameState { SplashScreen, Playing }
public GameState currentState;
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);
}
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的LoadContent中有了这个
public enum GameState { SplashScreen, Playing }
public GameState currentState;
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);
}
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中更新
public enum GameState { SplashScreen, Playing }
public GameState currentState;
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);
}
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);
}
}
公共类SplashScreen
{
纹理2d-tSplash;
Vector2 vSplash=Vector2.0;
Game1 main=新Game1();
公共void加载内容(ContentManager内容)
{
tSplash=内容物荷载(“飞溅”);
}
公开作废更新(游戏时间游戏时间)
{
if(Keyboard.GetState().IsKeyDown(Keys.X))
{
main.currentState=Game1.GameState.Playing;
}
}
公共作废抽签(SpriteBatch SpriteBatch)
{
spriteBatch.Draw(tSplash、vSplash、Color.White);
}
}
还有我的演奏
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);
}
}
课堂游戏
{
纹理2d-tBack;
Vector2 vBack=Vector2.0;
Game1 mainG=新Game1();
公共无效初始化()
{
}
public void LoadContent(ContentManager ContentManager)
{
tBack=contentManager.Load(“播放”);
}
公开作废更新(游戏时间游戏时间)
{
}
公共作废抽签(SpriteBatch SpriteBatch)
{
spriteBatch.Draw(tBack、vBack、Color.White);
}
}
SplashScreen图像将显示,但当我按X时,平台状态不会出现。您正在为每个屏幕声明一个全新的
游戏对象。永远不要这样做。一个游戏
代表您的整个游戏
但是,可以对单个游戏对象进行多个引用
将一个游戏
对象保持在最顶层;你已经有了它来保存你的屏幕对象。您甚至可以在屏幕中保留main
和mainG
变量,但让它们指向您的单个游戏
:
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);
}
这将是一个很好的第一步,尽管我建议更进一步,为所有屏幕创建一个基类。检查游戏屏幕管理,让你去