简体   繁体   English

如何在 C# 中插入“if”语句? [等候接听]

[英]How do I thread in “if ” statements in C#? [on hold]

I am trying to make a racing game in C#.我正在尝试在 C# 中制作赛车游戏。 I have enemy cars coming at you and when they go past you and another one spawns in it is instant.我有敌车向你驶来,当他们 go 从你身边经过时,另一辆车会立即生成。 I want it to have a 1 second delay.我希望它有 1 秒的延迟。

I have tried using Thread.Sleep(1000);我试过使用Thread.Sleep(1000); and System.Threading.Thread.Sleep(1000);System.Threading.Thread.Sleep(1000); (Even though I think those are the same things). (即使我认为这些是相同的东西)。

My current code is:我目前的代码是:

if (EnemyCar1.Top >= 500)
    {
       e_X = enemyPos.Next(5, 200);
       e_Y = enemyPos.Next(5, 395);
       EnemyCar1.Location = new Point(e_X, e_Y);
       System.Threading.Thread.Sleep(1000);
    }

When I try to thread it pauses the whole game and I nonthing can move.当我尝试穿线时,它会暂停整个游戏,我什么也不能移动。

I expected it to thread just the one method but it actually paused everything in the game, which is not what I wanted.我希望它只线程化一种方法,但它实际上暂停了游戏中的所有内容,这不是我想要的。

I think currently this will not work.我认为目前这行不通。 Because you execute the Sleep Function on the UI Thread.因为您在 UI 线程上执行 Sleep Function。

If you create a thread for every Car so you can let the cars "sleep".如果您为每辆汽车创建一个线程,那么您可以让汽车“休眠”。

btw: Think about using Tasks顺便说一句:考虑使用任务

You can use System.Diagnostics.Stopwatch and bind the spawn as an event which occurs when it reaches 1 second.您可以使用 System.Diagnostics.Stopwatch 并将 spawn 绑定为在达到 1 秒时发生的事件。 Stopwatch is automaticaly threaded.秒表是自动穿线的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM