一旦 GameState 从主菜单更改为正在播放,XNA 4 对象就不会移动
XNA 4 objects not moving once GameState has changed from the main menu to playing
在 GameState.MainMenu 时一切正常,但当我单击 btnPlay (DCButton Class) 时,它切换到 GameState.Playing(加载 map/player),但没有任何动画案例 GameState.Playing.
protected override void Update(GameTime gameTime)
{
MouseState mouse = Mouse.GetState();
switch (CurrentGameState)
{
case GameState.MainMenu:
if (btnPlay.isClicked == true) CurrentGameState = GameState.Playing;
btnPlay.Update(mouse);
if (btnOptions.isClicked == true) CurrentGameState = GameState.Options;
btnOptions.Update(mouse);
if (btnExit.isClicked == true) Exit();
btnExit.Update(mouse);
break;
case GameState.Options:
break;
case GameState.Playing:
player.Update();
// Exit();
break;
}
base.Update(gameTime);
}
DCPlayer (Class) Update() 方法包括获取键盘输入和改变播放器的位置。我也试过这个:
case GameState.Playing:
player.Position.X += 1; // <- Position is a Vector2
break;
但这也不会为 sprite 设置动画。有谁知道为什么会这样?
注意:CurrentGameState 是一个名为 GameState 的枚举
编辑:绘图()
protected override void Draw(GameTime gameTime)
{
GraphicsDevice.Clear(Color.CornflowerBlue);
spriteBatch.Begin();
switch (CurrentGameState)
{
case GameState.MainMenu:
spriteBatch.Draw(mmBackgroundTexture, mmBackground, Color.White);
btnPlay.Draw(spriteBatch);
btnOptions.Draw(spriteBatch);
btnExit.Draw(spriteBatch);
break;
case GameState.Options:
spriteBatch.Draw(mmBackgroundTexture, mmBackground, Color.White);
break;
case GameState.Playing:
int progress = level.GetLevelProgress();
level.level = progress;
level.LoadLevel(); // <- This is where the map gets loaded from the map file
level.Draw(spriteBatch, Content); // <- This is were the map gets rendered
player = new DCPlayer(Content, level.grid, graphics.GraphicsDevice); // <- Could this be the problem, I initialized it here because the grid only gets initialized when the map is loaded
player.Draw(spriteBatch, Content); // <- Draw player in grid using map information (Vector2 DCLevel.Start)
break;
}
spriteBatch.End();
base.Draw(gameTime);
}
在你的绘制方法中,你每次都将玩家重新分配给一个 new 实例,这意味着它永远没有机会使用它的实际位置。根据您显示的代码的外观,您可以从 Draw
方法
中删除这一行
player = new DCPlayer(Content, level.grid, graphics.GraphicsDevice);
在 GameState.MainMenu 时一切正常,但当我单击 btnPlay (DCButton Class) 时,它切换到 GameState.Playing(加载 map/player),但没有任何动画案例 GameState.Playing.
protected override void Update(GameTime gameTime)
{
MouseState mouse = Mouse.GetState();
switch (CurrentGameState)
{
case GameState.MainMenu:
if (btnPlay.isClicked == true) CurrentGameState = GameState.Playing;
btnPlay.Update(mouse);
if (btnOptions.isClicked == true) CurrentGameState = GameState.Options;
btnOptions.Update(mouse);
if (btnExit.isClicked == true) Exit();
btnExit.Update(mouse);
break;
case GameState.Options:
break;
case GameState.Playing:
player.Update();
// Exit();
break;
}
base.Update(gameTime);
}
DCPlayer (Class) Update() 方法包括获取键盘输入和改变播放器的位置。我也试过这个:
case GameState.Playing:
player.Position.X += 1; // <- Position is a Vector2
break;
但这也不会为 sprite 设置动画。有谁知道为什么会这样?
注意:CurrentGameState 是一个名为 GameState 的枚举
编辑:绘图()
protected override void Draw(GameTime gameTime)
{
GraphicsDevice.Clear(Color.CornflowerBlue);
spriteBatch.Begin();
switch (CurrentGameState)
{
case GameState.MainMenu:
spriteBatch.Draw(mmBackgroundTexture, mmBackground, Color.White);
btnPlay.Draw(spriteBatch);
btnOptions.Draw(spriteBatch);
btnExit.Draw(spriteBatch);
break;
case GameState.Options:
spriteBatch.Draw(mmBackgroundTexture, mmBackground, Color.White);
break;
case GameState.Playing:
int progress = level.GetLevelProgress();
level.level = progress;
level.LoadLevel(); // <- This is where the map gets loaded from the map file
level.Draw(spriteBatch, Content); // <- This is were the map gets rendered
player = new DCPlayer(Content, level.grid, graphics.GraphicsDevice); // <- Could this be the problem, I initialized it here because the grid only gets initialized when the map is loaded
player.Draw(spriteBatch, Content); // <- Draw player in grid using map information (Vector2 DCLevel.Start)
break;
}
spriteBatch.End();
base.Draw(gameTime);
}
在你的绘制方法中,你每次都将玩家重新分配给一个 new 实例,这意味着它永远没有机会使用它的实际位置。根据您显示的代码的外观,您可以从 Draw
方法
player = new DCPlayer(Content, level.grid, graphics.GraphicsDevice);