【问题标题】:AHK Continue after PixelSearchAHK 在 PixelSearch 之后继续
【发布时间】:2020-12-11 00:21:34
【问题描述】:

我只想让脚本仅在成功的 PixelSearch 后继续。即。

  • 重复 1-3 直到 PixelSearch 成功
  • 重复 4-6 直到 PixelSearch 成功
  • 重复 7-9 直到 PixelSearch 成功,中断

这就是我现在所拥有的。我用什么代替休息?


;stuff
PixelSearch, Px, Py, 995, 256, 999, 262, 0x84BCD1, 40, Fast
if ErrorLevel = 0
    break //???
;stuff
PixelSearch, Px, Py, 995, 256, 999, 262, 0x84BCD1, 40, Fast
if ErrorLevel = 0
    break //???
;stuff
PixelSearch, Px, Py, 995, 256, 999, 262, 0x84BCD1, 40, Fast
if ErrorLevel = 0
    break
}

【问题讨论】:

  • 能否请您发布您的整个脚本以及脚本应该做什么?目前,我看不到您在问题中提到的循环,也不了解该脚本的上下文/该脚本应该作为一个整体做什么。
  • 我不喜欢明显的正确答案不被 OP 接受

标签: autohotkey


【解决方案1】:

简答:您希望将break 替换为return

长答案BreakContinue 在您尝试退出任何类型的 loopable statement 时使用。为了停止脚本的大多数其他部分的执行,例如auto-execute sectionhotkeysubroutinefunction,您可以使用return 语句。 此外,我认为您的脚本的其他一些部分并没有按照您的预期工作。例如,在您当前使用 ErrorLevel 的情况下,只有在屏幕上发现 none 个提到的像素时,您的脚本才会有效地继续。这是因为您检查程序是否应该停止执行的条件语句正在检查 if ErrorLevel = 0。从先前链接的文档中,仅当成功找到像素时,ErrorLevel 才会为 0,这意味着如果未找到像素,则将返回非零值。为了在您的代码中解决此问题,我们只需将代码中出现的if ErrorLevel = 0 更改为if ErrorLevel

修改后的代码

;Move other stuff up here
PixelSearch, Px, Py, 995, 256, 999, 262, 0x84BCD1, 40, Fast
if ErrorLevel
    return
;stuff
PixelSearch, Px, Py, 995, 256, 999, 262, 0x84BCD1, 40, Fast
if ErrorLevel
    return
;stuff
PixelSearch, Px, Py, 995, 256, 999, 262, 0x84BCD1, 40, Fast
if ErrorLevel
    return
MsgBox, complete
;put code that you want to run after the condition is met here
return

【讨论】:

    【解决方案2】:

    注意:这是在澄清之前对问题版本的回答。我已经发布了一个新的答案,应该可以更好地解决这个问题。

    根据我的理解,这是一个 GoSub 解决方案,如果 PixelSearch 无法找到像素/ErrorLevel = 0,则运行子程序:

    PixelSearch, Px, Py, 995, 256, 999, 262, 0x84BCD1, 40, Fast
    if ErrorLevel = 0
        gosub, subroutine 
    
    PixelSearch, Px, Py, 995, 256, 999, 262, 0x84BCD1, 40, Fast
    if ErrorLevel = 0
        gosub, subroutine
    
    subroutine:
    ;insert whatever code you want to run in between the line above and the return
    MsgBox, This is a subroutine. No matching pixels were found
    return
    

    【讨论】:

    • 正如写的那样,这个 GoSub 会在 PixelSearch 成功还是不成功时?我试图让它按如下方式运行:重复 1-3 直到 PixelSearch 成功 重复 5-8 直到 PixelSearch 成功 重复 10-13 直到 PixelSearch 成功,然后中断
    • 我已经编辑了我的帖子以澄清。我很抱歉一开始让事情变得如此混乱。
    • @vtDemo 我明白了,这不会做你想做的事。我将编写一个新脚本来解决这个明确的问题。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-12-29
    • 2012-07-17
    • 2010-12-03
    • 1970-01-01
    • 1970-01-01
    • 2019-07-10
    相关资源
    最近更新 更多