Pascal 内部的If语句重复直到循环
我正在使用SwingGame库创建一个切换按钮。现在我正在努力让它切换。那块看起来像这样:Pascal 内部的If语句重复直到循环,pascal,swingame,Pascal,Swingame,我正在使用SwingGame库创建一个切换按钮。现在我正在努力让它切换。那块看起来像这样: var clr: Color; clicked: Boolean; boolcheck: String; begin OpenGraphicsWindow('Toggle button test', windowsWidth, windowsHeight); clr := ColorWhite; clicked := true; boolcheck := 't
var
clr: Color;
clicked: Boolean;
boolcheck: String;
begin
OpenGraphicsWindow('Toggle button test', windowsWidth, windowsHeight);
clr := ColorWhite;
clicked := true;
boolcheck := 'true';
repeat
ClearScreen(clr);
ProcessEvents();
//Play button and Pause button
if (ButtonClicked(buttonX-buttonRadius, buttonY-buttonRadius, buttonRadius*2, buttonRadius*2) = true) and (clicked = true) then
begin
clr := ColorRed;
clicked := false;
boolcheck := 'false';
DrawAButton();
end
else if (ButtonClicked(buttonX-buttonRadius, buttonY-buttonRadius, buttonRadius*2, buttonRadius*2) = true) and (clicked = false) then
begin
clr := ColorBlue;
clicked := true;
boolcheck := 'true';
DrawADifferentButton();
end;
DrawText(echo, ColorBlack, 'arial.ttf', 14, 55, 55);
RefreshScreen();
until WindowCloseRequested();
end;
基本上,我打算这样做,如果用户通过ButtonClicked()
(swinggame函数)单击窗口的此区域,并且单击的变量为false,则背景颜色将为红色,如果不是,则为蓝色。但由于某种原因,我只能把它改成红色,蓝色根本没有出现。我通过创建一个boolcheck
变量进行了一些故障排除,我看到该变量一直被设置为true,我确实看到它在几秒钟内变为false,然后又变回true……但是我没有将单击的
变量初始定义放在循环中,那么为什么它不保持false呢
编辑:这里是对点击的按钮的定义
function ButtonClicked(posX, posY: Single; w, h: Integer): Boolean;
var
x, y: Single;
begin
x := MouseX();
y := MouseY();
result := false;
if MouseClicked(LeftButton) then
begin
if (x >= posX) and (x <= w + posX) and (y >= posY) and (y <= h + posY) then
begin
result := true;
end;
end;
end;
函数按钮点击(posX,posY:Single;w,h:Integer):布尔;
变量
x、 y:单身;
开始
x:=MouseX();
y:=MouseY();
结果:=假;
如果鼠标单击(左键),则
开始
如果(x>=posX)和(x=posY)和(y好的,多亏了@Lower的建议,我解决了这个问题。在花了一些时间考虑@Lower的建议后,我意识到一旦程序被重置,它将以单击开始,再次处于0。所以我必须做的是,使按钮被单击检查返回1
或的函数>0
,或true
或false
在Main()
中单击。这样,单击的将始终更新,并且不会重置过程,因为单击的时间始终为0
function Toggle(clicked): Integer;
if ButtonClicked(buttonX-buttonRadius, buttonY-buttonRadius, buttonRadius*2, buttonRadius*2) then
if (clicked := true) then
begin
clr := ColorRed;
result := false;
DrawAButton();
end
else
begin
clr := ColorBlue;
result := true;
DrawADifferentButton();
end;
then in `Main()` I would call it as follow:
//Stuffs
begin
clicked := true;
repeat
clicked := Toggle(clicked);
//Other stuffs
也许问题出在ButtonClicked
?每个ButtonClicked
按钮的状态在什么时候被重置?顺便说一下,您可以简化逻辑的外观和流程。如果foo=true,那么…
与简单的如果foo-then
相同,因为foo
是一个布尔值。同样,如果foo=false,则
与相同,如果不是foo,则
。最后如果A和B,则X else如果A和B,则Y
可以写入如果A,则如果B,则X else Y
。其中A和B是布尔表达式,X和Y是代码块。谢谢你的建议。关于函数……我不明白你的意思当你说它在什么状态下被重置时……我已经添加了点击的函数按钮。希望这会有所帮助。`if(clicked:=true)然后`=>如果点击然后
(:=
在这里无论如何都是一个错误)。你也不需要函数切换
。只需点击就可以了;
。