降低 XNA 中的运行速度

发布于 2024-10-07 01:01:00 字数 168 浏览 0 评论 0原文

我想知道是否有办法降低运行速度(调用之间的更新和绘制时间)。

我需要减慢调用的频率,因为它们发生得太快,结果是在很短的时间内显示我需要显示的内容,如此之短以至于用户实际上看不到任何内容。

目标是更缓慢地显示代表我的应用程序逻辑的进度。

如果标题不清楚,我参考XNA游戏框架。

i wan't to know if there is a way to decrease the Run Speed (Update and Draw time between calls).

I need to slow down the frequency of calls because they occurs too fast, and that has the consequence of show what i need to show in very little time, so little that actually the user can't see anything.

The objective is show more slowly the progress of what represent the logic of my application.

If the title isn't clear, i refer to the XNA Game Framework.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

后知后觉 2024-10-14 01:01:00

我建议您在 Update 方法中添加一些代码,每 x 秒更新一次游戏逻辑。

定义:

int millisecondsPerFrame = 1000 //Update every 1 second
int timeSinceLastUpdate = 0 //Accumulate the elapsed time

现在,在您的更新中,您可以得到类似的内容:

timeSinceLastUpdate += gameTime.ElapsedGameTime.TotalMilliseconds;
if(timeSinceLastUpdate >= millisecondsPerFrame)
{
     timeSinceLastUpdate = 0;

     //YOUR GAMES LOGIC GOES HERE
}

通过这种方法,您不必减慢整个程序的速度。

I would recommend you to have some code inside your Update method that updates your game´s logic every x seconds.

Define:

int millisecondsPerFrame = 1000 //Update every 1 second
int timeSinceLastUpdate = 0 //Accumulate the elapsed time

Now, on your update you can have something like:

timeSinceLastUpdate += gameTime.ElapsedGameTime.TotalMilliseconds;
if(timeSinceLastUpdate >= millisecondsPerFrame)
{
     timeSinceLastUpdate = 0;

     //YOUR GAMES LOGIC GOES HERE
}

With this approach you dont have to slow down the entire program.

墨小墨 2024-10-14 01:01:00

我在MSDN中找到了答案。关键是设置 true IsFixedTimeStep,然后增加或减少变量 TargetElapsedTime

首先,我需要将属性 IsFixedTimeStep 设置为 true,然后我可以使用变量 TargetElapsedTime(调用 Update 的速率),默认值为 1/60 秒,对于我需要显示的内容来说太快了,所以我将其设置为 1/2 秒,并添加两个键来在运行时修改此值。

IsFixedTimeStep = true;
TargetElapsedTime = TimeSpan.FromSeconds(0.5);

然后在更新方法中,我可以使用 TargetElapsedTime 来加速和减慢更新调用:

if ((Keyboard.GetState().IsKeyDown(Keys.W)))
{
    TargetElapsedTime += TimeSpan.FromSeconds(0.1f);
}
if ((Keyboard.GetState().IsKeyDown(Keys.S)))
{
   if ((TargetElapsedTime - TimeSpan.FromSeconds(0.1f))>TimeSpan.Zero)
   {
       TargetElapsedTime -= TimeSpan.FromSeconds(0.1f);    
   }

}

I found the answer in MSDN. The key is set true IsFixedTimeStep and then increase or decrease the variable TargetElapsedTime

First I need to set to true the property IsFixedTimeStep, then I could play with the variable TargetElapsedTime(rate at what Update is call) that for default is 1/60 sec, too fast for what I need to show, so i set it in 1/2 sec, and add two keys to modify this value in runtime.

IsFixedTimeStep = true;
TargetElapsedTime = TimeSpan.FromSeconds(0.5);

And then in the update method I could play with the TargetElapsedTime to speed Up and slow Down the update calls:

if ((Keyboard.GetState().IsKeyDown(Keys.W)))
{
    TargetElapsedTime += TimeSpan.FromSeconds(0.1f);
}
if ((Keyboard.GetState().IsKeyDown(Keys.S)))
{
   if ((TargetElapsedTime - TimeSpan.FromSeconds(0.1f))>TimeSpan.Zero)
   {
       TargetElapsedTime -= TimeSpan.FromSeconds(0.1f);    
   }

}
南七夏 2024-10-14 01:01:00

我同意这里的其他一些答案。您真的不想减慢游戏的执行速度。这样做没有什么意义。另外,当你被困在那个恒定的时间里时,后期的设计会变得更加困难。

使用时间增量的问题是您永远无法确定更新方法将被调用的频率。有很多因素可能会导致运行速度太快或太慢。如果您的游戏循环运行缓慢,即使只有一小部分,您也可能会错过游戏中的整个滴答声。对于体育比赛之类的事情来说,这会很糟糕。

而是使用计时器。 .NET 有一个令人惊叹的事件模型和一个预构建的计时器类。它被初始化,你告诉它你想要循环的频率。每个循环都会触发一个您可以收听的事件。这样做的优点有两个。首先,您可以放心,您的蜱虫会在您要求的时间附近或在您要求的时候触发。其次,它不在主游戏逻辑中运行,从而使您的程序能够以更好的方式构建。您甚至可以将多个事件绑定到计时器,从而使您的代码更加模块化。

试试这个...

//create a new instance of a timer 
//and set it to 1000 millisecond interval(1 second)
Timer t = new Timer(1000);

//Add a listener to the elapsed event
t.Elapsed += new ElapsedEventHandler(t_Elapsed);

//start the timer
t.Start();

void t_Elapsed(object sender, ElapsedEventArgs e)
{
    // I am called each second
}

I agree with some of the other answers here. You don't really want to slow the execution of the game down. There is little point in doing this. Plus design later down the track becomes harder as you are stuck with that constant time.

The issue with using time delta's is that you can never be sure how often your update method will be called. There are any number of factors that might cause this to run too fast or too slow. If your game loop runs slow even for a fraction you could miss an entire tick in your game. For something like a sports game this would be bad.

Instead use a timer. .NET has an amazing event model and a pre built timer class. It gets initialized and you tell it how often you want to loop. Each loop will fire an event you can listen to. The advantages of this are two fold. Firstly you can be assured that your tick will fire close to or when you ask it to. Second it does not run in the main game logic allowing your programs to be structured in a better way. You can even bind more than one event to the timer allowing you to me more modular with your code.

Try this...

//create a new instance of a timer 
//and set it to 1000 millisecond interval(1 second)
Timer t = new Timer(1000);

//Add a listener to the elapsed event
t.Elapsed += new ElapsedEventHandler(t_Elapsed);

//start the timer
t.Start();

void t_Elapsed(object sender, ElapsedEventArgs e)
{
    // I am called each second
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文