我正在做一个简单的计数器(从 1 到 10 秒计数)并且正在工作,但不在屏幕上。它会在几秒钟内完成计数(1, 2,..., 10),但当达到 10 时,屏幕上只显示 10!
这是添加计数器文本的函数:
public function updateCounter(counter:Int)
{
background.drawRect(0, 19, FlxG.width, 1, FlxColor.CYAN);
add(background);
textcounter.text = Std.string(counter);
textcounter.x = textcounter.width - 4;
add(textcounter);
trace("In updateCounter:" + counter);
}
从这里调用该函数:
override public function update(elapsed:Float)
{
super.update(elapsed);
while (counterStarted == true && count < 10)
{
if (second == lastSecond)
{
today = Date.now();
second = today.getSeconds();
}
if (second != lastSecond)
{
lastSecond = second;
count++;
counter++;
counterHud.updateCounter(counter);
trace(count);
trace("secound was != last second");
}
}
}
我试图让屏幕上的计数器从 1 计数到 10。
while
中的update()
强制执行单帧中的所有代码:这将导致draw()
函数仅显示计数器的最后一个值。您应该将其更改为在许多帧上执行。
尝试将其更改为
if
:
override public function update(elapsed:Float)
{
super.update(elapsed);
if (counterStarted == true && count < 10) // <-- change this line
{
if (second == lastSecond)
{
today = Date.now();
second = today.getSeconds();
}
if (second != lastSecond)
{
lastSecond = second;
count++;
counter++;
counterHud.updateCounter(counter);
trace(count);
trace("secound was != last second");
}
}
}
这样,每次调用
update()
函数(即每一帧)时,都会运行检查并更新计数器。