0

在我的编程中,我制作了两个攻击日志(它们是标签),它们显示了战斗中发生的事情......但是每当我运行代码时,两个标签都会跳过所有以前的代码并显示它被告知要显示的最后一件事。 ..

this -> EnemyAttackLog -> Text = "[Enemy Attack Log]   " + EName + " Attacked!!";
             Sleep(500);
             damage = rnd4 -> Next(-2,3) + EnemyA;
             if (D > EnemyA)
             {
                 damage = rnd4 -> Next(3,6);
                 this -> AttackLog -> Text = "[Attack Log]    Your Armor Reduced the Damage!";
             }
             Sleep(500);
             this -> EnemyAttackLog -> Text = "[Enemy Attack Log]   " + EName + " did " + damage + " Damage";
             Hp -= damage;
             this -> HealthBar -> Value = Hp;
             Sleep(800);
             damage = rnd4 -> Next(-2,3) + A;
             if (EnemyD > A)
             {
                 damage = rnd4 -> Next(3,6);
                 this -> EnemyAttackLog -> Text = "[Enemy Attack Log]    " + EName + "'s armor reduced damage!";
             }
             Sleep(1500);
             this -> AttackLog -> Text = "[Attack Log]   " + Name + " did " + damage + " Damage";
             EnemyHp -= damage;
             this -> EnemyHealth -> Value = EnemyHp;

如果这做了我认为它会做的事情,它会显示

this -> EnemyAttackLog -> Text = "[Enemy Attack Log]   " + EName + " Attacked!!";

半秒,然后继续显示下一个代码,再等半秒,但是,它所做的只是等待所有的睡眠功能(加起来是 3300 毫秒),然后在最后显示文本.. . 这是

this -> AttackLog -> Text = "[Attack Log]   " + Name + " did " + damage + " Damage";

为什么它会跳过所有先前的命令(对于标签文本)但仍然等待 3300 毫秒?

4

1 回答 1

0

这可能是因为您正在休眠 UI 线程。我刚刚在 C# 中尝试过类似的测试,但遇到了同样的问题。要解决此问题,您必须在设置值后在文本框上调用 Refresh。

另一种方法是在后台线程上完成工作,然后将“this->EnemyAttackLog->Text = sometext”代码编组到 UI 线程上。这样,您的 UI 将保持响应。

于 2013-06-08T06:42:09.867 回答