C# 游戏时间与实时
我正在尝试用3秒钟的实时时间填充一个矩形C# 游戏时间与实时,c#,xna,xna-4.0,C#,Xna,Xna 4.0,我正在尝试用3秒钟的实时时间填充一个矩形 我希望增量是一个常量值,看起来很漂亮,没有加速度 我很难理解该做什么 这是我的代码 // constant // 1.0f = 100% of rectangle, 3 sec = 3000.0 miliseconds float addValue = 1.0f/3000.0f; public override void Update(GameTime gameTime) { newGameTime += gameTi
我希望增量是一个常量值,看起来很漂亮,没有加速度
我很难理解该做什么
这是我的代码
// constant
// 1.0f = 100% of rectangle, 3 sec = 3000.0 miliseconds
float addValue = 1.0f/3000.0f;
public override void Update(GameTime gameTime)
{
newGameTime += gameTime.ElapsedGameTime.Milliseconds;
// once the percentage is set to 0 this starts
if ((percentage < 1))
{
// calculate value here in order to time
percentage += addValue;
}
}
//常数
//1.0f=矩形的100%,3秒=3000.0毫秒
浮动附加值=1.0f/3000.0f;
公共覆盖无效更新(游戏时间游戏时间)
{
newGameTime+=gameTime.ElapsedGameTime.ms;
//将百分比设置为0后,此操作开始
如果((百分比<1))
{
//在此处计算值,以便计算时间
百分比+=附加值;
}
}
我一直在尝试各种疯狂的数学来把它弄对,但我完全失去了它(
我知道我应该使用gameTime或newGameTime,但我迷路了我想那是你的更新/渲染功能 比如说,自上次渲染以来,已经过了300毫秒。这意味着必须向矩形中添加100%/3000ms*300ms=10% ->计算中缺少经过的时间:
percentage += addValue * gameTime.ElapsedGameTime.Milliseconds;
根据ccKep刚才在评论中提到的,我可能完全错了这个答案 但为了以防万一,这就是你要找的,我把它放在一起了 其主要思想是让一个计时器事件控制增量。即使我提交的代码不合适,这个想法也可能适用
public int percentage;
public int Percentage
{
get { return percentage; }
set
{
percentage = value;
if (Percentage >= 0 && Percentage < 100)
{
progressBar1.Value = value;
}
else
{
Percentage = 0;
timer1.Stop();
}
}
}
private void button1_Click(object sender, EventArgs e)
{
Percentage = 0;
timer1.Interval = 1000;
timer1.Start();
}
private void timer1_Tick(object sender, EventArgs e)
{
double addValue = 100 / 3;
Percentage += (int)addValue;
}
public int百分比;
公共整数百分比
{
获取{返回百分比;}
设置
{
百分比=价值;
如果(百分比>=0&&Percentage<100)
{
progressBar1.值=值;
}
其他的
{
百分比=0;
timer1.Stop();
}
}
}
私有无效按钮1\u单击(对象发送者,事件参数e)
{
百分比=0;
计时器1。间隔=1000;
timer1.Start();
}
私有无效计时器1_刻度(对象发送方,事件参数e)
{
双加值=100/3;
百分比+=(int)附加值;
}
根据我从问题中收集到的信息,我认为他希望矩形填充3个“实心”步骤,而不是常规的进度条。至少我认为他打算这样做。他要求在3“实时秒”内填充它。我假设Update函数被调用的速率取决于FPS,因此他必须计算自上次更新以来经过的时间,并使用该时间来计算进度。这取决于您是否使用FixedTimeStep。使用时间的任何一种方法都不是一个坏主意;FixedTime只是确保时间值一致。我不能使用新游戏时间增量来进行这些计算?在我将elepsedTime添加到addvalue之后,该条填充得太快,不需要3秒钟,就像3毫秒一样-编辑-
好的,我再次检查,似乎我在另一个类中设置了错误的addvalue…现在终于可以正常工作了。
谢谢