如何在 XNA 中倒计时时创建类似暂停的效果
How to create a pause like effect while counting down in XNA
我正在创建一个简单的游戏,每当我离开菜单时,我想要一个 3 秒的计时器来覆盖屏幕,同时游戏暂停。我曾想过我会做这样的事情
protected void Draw(GameTime gameTime)
{
//..//
for(int i = 3; i > 0; i--)
{
spriteBatch(gameFont, i.ToString(), new Vector2(GraphicsDevice.Viewport.Width / 2, GraphicsDevice.Viewport.Height / 2), Color.White);
Thread.Sleep(1000);
}
//..//
}
但是,这只会在菜单上暂停三秒钟,然后立即让您进入游戏。你可以在一瞬间看到一个随机数,但根本看不到真正的倒计时。我该怎么做才能暂停游戏 运行 而游戏仍然平局?
Thread.Sleep(1000);
在最好的时候你想避免使用 Thread.Sleep()
我想不出 任何时候 你应该在 游戏。游戏应该 运行 全力以赴,并且您想尽量减少 CPU 阻塞或休眠 的情况。
What can I do to pause the games running and still draw the game?
通常在游戏中(XNA 也不例外),绘图代码 与状态更新 机制是分开的。这意味着您的 Draw()
应该专注于绘画而不是其他。
暂停 可以被视为游戏 状态 并且最好在游戏的 Update()
.[=20 中进行处理=]
考虑在您的游戏中添加一个状态,也许:
enum States
{
Initialising,
InMenu,
LeavingMenu,
InGame,
}
States _state;
然后在您的 Update()
中,您可以在状态机上工作,从菜单中切换;离开菜单;终于在游戏中:
DateTime timeOfEscPressed;
protected virtual void Update(GameTime gameTime)
{
switch (_state)
{
case States.Initialising:
break;
case States.InMenu:
// *** pseudo-code here ***
// if ESC pressed then
// _state = States.LeavingMenu
// timeOfEscPressed = DateTime.Now;
break;
case States.LeavingMenu:
// stay in this state for 3 seconds
if ((DateTime.Now - timeOfEscPressed).TotalSeconds >= 3)
{
_state = States.InGame;
}
break;
case States.InGame:
if (menuKeyPressed) // pseudo code
{
_state = States.InMenu;
timeOfEscPressed = DateTime.Now;
}
break;
}
}
你的抽奖代码就是这样,抽奖代码:
protected void Draw(GameTime gameTime)
{
switch (_state)
{
case States.InMenu:
DrawMenu();
break;
case States.LeavingMenu:
ShowTimer();
break;
}
}
通过这种方式,您可以获得游戏暂停但不会冻结更新绘制循环的错觉。
我正在创建一个简单的游戏,每当我离开菜单时,我想要一个 3 秒的计时器来覆盖屏幕,同时游戏暂停。我曾想过我会做这样的事情
protected void Draw(GameTime gameTime)
{
//..//
for(int i = 3; i > 0; i--)
{
spriteBatch(gameFont, i.ToString(), new Vector2(GraphicsDevice.Viewport.Width / 2, GraphicsDevice.Viewport.Height / 2), Color.White);
Thread.Sleep(1000);
}
//..//
}
但是,这只会在菜单上暂停三秒钟,然后立即让您进入游戏。你可以在一瞬间看到一个随机数,但根本看不到真正的倒计时。我该怎么做才能暂停游戏 运行 而游戏仍然平局?
Thread.Sleep(1000);
在最好的时候你想避免使用 Thread.Sleep()
我想不出 任何时候 你应该在 游戏。游戏应该 运行 全力以赴,并且您想尽量减少 CPU 阻塞或休眠 的情况。
What can I do to pause the games running and still draw the game?
通常在游戏中(XNA 也不例外),绘图代码 与状态更新 机制是分开的。这意味着您的 Draw()
应该专注于绘画而不是其他。
暂停 可以被视为游戏 状态 并且最好在游戏的 Update()
.[=20 中进行处理=]
考虑在您的游戏中添加一个状态,也许:
enum States
{
Initialising,
InMenu,
LeavingMenu,
InGame,
}
States _state;
然后在您的 Update()
中,您可以在状态机上工作,从菜单中切换;离开菜单;终于在游戏中:
DateTime timeOfEscPressed;
protected virtual void Update(GameTime gameTime)
{
switch (_state)
{
case States.Initialising:
break;
case States.InMenu:
// *** pseudo-code here ***
// if ESC pressed then
// _state = States.LeavingMenu
// timeOfEscPressed = DateTime.Now;
break;
case States.LeavingMenu:
// stay in this state for 3 seconds
if ((DateTime.Now - timeOfEscPressed).TotalSeconds >= 3)
{
_state = States.InGame;
}
break;
case States.InGame:
if (menuKeyPressed) // pseudo code
{
_state = States.InMenu;
timeOfEscPressed = DateTime.Now;
}
break;
}
}
你的抽奖代码就是这样,抽奖代码:
protected void Draw(GameTime gameTime)
{
switch (_state)
{
case States.InMenu:
DrawMenu();
break;
case States.LeavingMenu:
ShowTimer();
break;
}
}
通过这种方式,您可以获得游戏暂停但不会冻结更新绘制循环的错觉。