如何使定时器保持乳宁while循环,等待另一次的结果? [英] How to make timer keep runing while loop wait for another time result?

查看:143
本文介绍了如何使定时器保持乳宁while循环,等待另一次的结果?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在我的代码下面的问题,
I有这个环上定时器运行(这是在大的定时器运行循环的只是一小部分),



这大计时器内(他剔每隔1秒)我有1方法需要等待5秒钟,则继续执行循环代码的休息,但我想它不会停留在代码和定时器继续运行每1秒,并不会等待那些5秒。



我做了什么我添加了一个新的计时器(timer_deva)是蜱每5秒,做它里面所有的检查,然后计时器停止。



所以我的问题是,我需要等待5秒检索一个值来完成我的代码,但我需要我的主定时器将继续同时运行,而当他得到他的结果对于其他时间,他需要完成他留下的代码。



在预先感谢

 否则如果(mobID.Equals(Convert.ToInt32(txtDeva)))
{
// START WITH TIMER
timer_deva CHECK 。开始();
// timer_deva更新winnerNation
// END检查计时器中的一些方法 - GET RESULT
冠军(了zoneid,winnerNation,humansKills,orcKills);
}


解决方案

TL;博士



常规计时器没有在游戏中使用。 。游戏有处理自己的逻辑和所经过的时间非常不同的机制



龙版本:



我知道这可能不会直接回答你的问题,但它的办法不多的文字抽筋成一个注释。你的声音计时器非常复杂和层次。从你的变量名,我会假设你正在编写游戏。游戏一般不与定时器或不在你所期望的方式工作。这种不同的游戏类似的行为会帮助你很多你的计时器的问题,甚至可能会帮助你更与一般的设计。



游戏通常都被称为东西游戏循环。一般来说它是被调用一次在一个循环后,其他三个主要功能:

 而(运行)
{
HandleUserInput();
ChangeWorld();
渲染();
}

您获得用户输入,则相应地改变游戏世界,你把它画到屏幕。现在,更快您的计算机,速度越快这个循环运行。这是很好的图形(想想FPS),但糟糕的比赛。想象一下,俄罗斯方块,每一个帧块移动。现在,我不想买一个更快的计算机,游戏将变得更加困难的方式。



因此​​,为了保持游戏的速度保持恒定,不受电脑的电源,循环认为过去的时间:

 而(运行)
{
VAR timePassedSinceLastLoop = CalculateTimeDelta() ;

HandleUserInput();
ChangeWorld(timePassedSinceLastLoop);
渲染();
}

现在想象在游戏中的东西冷却时间。玩家按下A,一些很酷的动作发生了,虽然他可以按下A再次,什么都不会发生在接下来的5秒。但游戏仍然运行并执行所有可能发生的游戏内的其他东西。这不是一个常规的定时器。这是一个变量,让我们把它叫做 ActionCooldown ,而一旦玩家触发操作,它设置为5秒。每当世界发生变化,timePassed是从数量,直到它的零减去。所有的时间,在游戏运行和处理输入和渲染。但只有一次 ActionCooldown 降为零,另按a将再次触发该操作。


i have the following issue in my code, i have this loop running on timer (this is just a small part of the loops that running on the big timer),

inside that big timer (he tick every 1 second) i have 1 method that need to wait 5 second then continue with the the rest of the loop code, but i want that it wont stuck the code and the timer will continue to run every 1sec and wont wait for those 5sec.

what i did i add a new timer (timer_deva) that tick every 5sec and did all the checks inside it, and then timer stops.

so my issue is that i need to wait 5sec to retrieve a value to complete my code, but i need that my main timer will keep running simultaneously, and when he get his result for the other time he will need to complete the code he left behind.

thanks in advance,

else if (mobID.Equals(Convert.ToInt32(txtDeva)))
{
    //START CHECK WITH TIMER
    timer_deva.Start();
    //Methods inside timer_deva update the winnerNation
    //END CHECK TIMER - GET RESULT
    winner(zoneId, winnerNation, humansKills, orcKills);
}

解决方案

tl;dr

Conventional Timers are not used in games. Games have a very different mechanism for handling their logic and the time that passed.

Long Version:

I know this may not answer your question directly, but it's way to much text to cramp into a comment. Your timers sound very complicated and hierarchical. From your variable names I will assume you are programming a game. Games normally don't work with timers or not in the way you would expect. This different game-like behaviour would help you a lot in your timers problem and may even help you more with your design in general.

Games normally have something called a game loop. Generally speaking it's three main functions that are called one after the other in a loop:

while(running)
{
    HandleUserInput();
    ChangeWorld();
    Render();
}

You get user input, you change the game world accordingly and you draw it to the screen. Now, the faster your computer is, the faster this loop runs. That's good for the graphics (think FPS), but bad for the game. Imagine Tetris where every frame the blocks move. Now I would not want to buy a faster computer, the game would get more difficult that way.

So to keep the game speed constant independent of the power of the computer, the loop considers the time passed:

while(running)
{
    var timePassedSinceLastLoop = CalculateTimeDelta();

    HandleUserInput();
    ChangeWorld(timePassedSinceLastLoop);
    Render();
}

Now imagine a cooldown for something in game. The player pressed "a", some cool action happened and although he may press "a" again, nothing will happen for the next 5 seconds. But the game still runs and does all the other things that may happen ingame. This is not a conventional timer. It's a variable, lets call it ActionCooldown, and once the player triggers the action, it's set to 5 seconds. Every time the world changes, the timePassed is subtracted from that number until it's zero. All the time, the game is running and handling input and rendering. But only once ActionCooldown hits zero, another press of "a" will trigger that action again.

这篇关于如何使定时器保持乳宁while循环,等待另一次的结果?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆