【问题标题】:GameTime and real timeGameTime 和实时
【发布时间】:2012-01-31 00:32:29
【问题描述】:

我正在尝试在 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 += gameTime.ElapsedGameTime.Milliseconds;

        // once the percentage is set to 0 this starts
        if ((percentage < 1))
        {
            // calculate value here in order to time
            percentage += addValue;
        }
    }

我一直在尝试各种疯狂的数学来让它正确,但我完全失去了它。 :( 我知道我应该使用 gameTime 或 newGameTime 但我迷路了

【问题讨论】:

    标签: c# xna xna-4.0


    【解决方案1】:

    我认为那是您的更新/渲染功能。

    假设,例如,自上次渲染以来,已经过去了 300 毫秒。这意味着您必须将 100%/3000ms * 300ms = 10% 添加到矩形中。

    ->您在计算中错过了经过的时间:

    percentage += addValue * gameTime.ElapsedGameTime.Milliseconds;
    

    【讨论】:

    • 从我可以从问题中收集到的信息,我认为他希望矩形填充 3 个“实心”步骤,而不是常规进度条。至少我认为他打算这样做。
    • 他要求在 3“实时秒”内填写。我会假设 Update 函数被调用的速率取决于 FPS,因此他必须花费自上次更新以来经过的时间并使用它来计算进度。
    • 这取决于您是否使用 FixedTimeStep。无论哪种方式使用时间都不是一个坏主意。 FixedTime 只是确保时间值是一致的。
    • 我不能使用 newGameTime 增量来进行这些计算吗?在我将 elepsedTime 添加到 addvalue 之后,条形填充太快了,它不需要 3 秒,而不是 3 毫秒
      -edit-
      好的,我再次检查,似乎我设置了错误的 addValue另一堂课……现在似乎终于可以正常工作了。
      谢谢
    【解决方案2】:

    根据 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;
        }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-10-21
      • 2017-05-02
      相关资源
      最近更新 更多