使用重复直到开始和停止动作

问题描述 投票:0回答:1

我想指定一个要重复的动作

使用名为 spbRewind 的按钮调用操作。按 spbRewind 后,进入倒带模式。 TimeForRewind 是使用设置为 10ms 的计时器设置的(因此理论上每 10ms 将重复一次操作)。

代码如下:

  TimeForRewind := Rewind and (CurrTime - PrevCallTime > IdealFrameTimeMSRewind);

  if TimeForRewind then
    begin
      repeat
        GotoSaveState(Game.CurrentIteration -4, 0);
      until ???;
    end;

我需要“直到???”是“直到再次按下按钮”,但我不确定该怎么做。

delphi repeat
1个回答
0
投票

你在评论中说的:

if TimeForRewind then
begin
  RewindTimer := TTimer.Create(self);
  RewindTimer.Interval := 200;
  RewindTimer.OnTimer := GotoSaveState(Max(Game.CurrentIteration-1, 0));
end;

看起来像是一场记忆灾难。每次运行此代码时,您都会创建一个新的

TTimer
,但您永远不会释放它们(或者也许您释放了?)

与其不断创建新的计时器并希望释放它们,不如在设计时或启动时创建一次计时器并将其禁用 (

Timer.Enabled = False
)。然后,当你需要它时,启用它,当你不需要它时,再次禁用它。

我了解到您想使用切换式按钮来控制它,例如:

procedure TYourForm.spbRewindClick(Sender: TObject);
begin
  TimeForRewind := not TimeForRewind; // Do you actually need this?
  RewindTimer.Enabled := TimeForRewind;
end;

评论后编辑

计时器在哪里?

假设您有一个表单,假设您称它为

MyForm
,最简单的方法是在设计时将
TTimer
组件从
Tool Palette
-
System
放到表单中(就像您为例如一个
TButton
)。将
Timer1
重命名为
RewindTimer
.

如何指定间隔?

Interval
属性设置为 200(即毫秒)并双击计时器控件,这将在您的表单中创建一个事件处理程序
procedure RewindTimerTimer(Sender: TObject);
声明,以及实现存根

procedure TMyForm.RewindTimerTimer(Sender: TObject);
begin

end;

您可以从中调用

GotoSaveState(...)
.

我猜 RewindTimer.Free;是防止内存问题所需的一切吗?

是的,但如果您将计时器集成到表单中则不需要。该表格将处理销毁。

E2010 不兼容类型:“TNotifyEvent”和“过程、无类型指针或无类型参数”

如果您按照第一点所述将计时器添加到表单中,这将会消失。

© www.soinside.com 2019 - 2024. All rights reserved.