我的游戏有问题。它分为两种状态:GameState.MainMenu
和GameState.Playing
。我想在播放时在屏幕上绘制计时器。我使用gameTime.TotalGameTime.Minutes
和
gameTime.TotalGameTime.Seconds
变量。但是当我点击按钮 Play Now,并且我的游戏从状态切换GameState.MainMenu
到GameState.Playing
. 时,计时器从 0 开始。它从我在 MainMenu 中花费的时间开始。我尝试创建下一个变量来计算我在 MainMenu 中花费的时间,并尝试从第一个变量中减去,但显示时间不正确。
我的减法时间:
minutesPlaying = gameTime.TotalGameTime.Minutes; ;
secondsPlaying = gameTime.TotalGameTime.Seconds;
switch (currentGameState)
{
case GameState.MainMenu:
minutesMenu = gameTime.TotalGameTime.Minutes;
secondsMenu = gameTime.TotalGameTime.Seconds;
if (btnPlay.isTapped == true)
{
currentGameState = GameState.Playing;
soundEffectInstance.Stop();
}
btnPlay.Update(collection);
break;
case GameState.Playing:
minutesTotal = minutesPlaying - minutesMenu;
secondsTotal = secondsPlaying - secondsMenu;
break;
}
调用我的方法:
timer.Update(minutesTotal, secondsTotal);
更新方法:
public void Update(int min, int sec)
{
string seconds, minutes;
seconds = sec.ToString();
minutes = min.ToString();
if (sec <= 9) seconds = "0" + seconds;
if (min <= 9) minutes = "0" + minutes;
nowString = minutes + ":" + seconds;
}
感谢您的回答:)