【问题标题】:AHK stuck with function with value outside of itAHK 坚持具有价值之外的功能
【发布时间】:2021-12-20 00:48:57
【问题描述】:

那些 if 不起作用,因为 test() 不会从外部获取价值,但是如果我将其放入内部,我不知道应该如何编写代码......

有什么建议吗?

SetTimer, test, 2000    ;I started off with 1000 then worked up to this number to confirm this was happening
number := 1
test:
    test()
return
test(){
    if WinActive("program") {   
    ImageSearch, foundX, foundY, 2000, 0, 2560, 1440, target.png
        if (ErrorLevel = 1) {
            if (number = 1) {
            CheckMap(z := "star.png")
            number := 2
            }
            if (number = 2) {
            CheckMap(z := "like.png")
            number := 3
            }
            if (number = 3) { 
            CheckMap(z := "question.png")
            number := 4
            }
            if (number = 4) {
            CheckMap(z := "cross.png")
            number := 1
            }
        } 
    }
}

CheckMap(x){  
    ImageSearch, foundX, foundY, 2000, 0, 2560, 1440, %x%
    if (ErrorLevel = 0) {
        MouseGetPos, StartX, StartY
        Click, %foundX%, %foundY%
        MouseMove, StartX, StartY
    }
}

*[::
Suspend
Pause, 1
return

*]::
ExitApp
return

【问题讨论】:

    标签: autohotkey


    【解决方案1】:

    找到了一些帮助,我需要静态数字,也修复了 if to else if,现在可以完美运行

    SetTimer, test, 2000    ;
    
    test:
        test()
    return
    test(){
        static number := 1
        if WinActive("program") {   
        ImageSearch, foundX, foundY, 2000, 0, 2560, 1440, target.png
            if (ErrorLevel = 1) {
                if (number = 1) {
                CheckMap(z := "star.png")
                number := 2
                } else if (number = 2) {
                CheckMap(z := "like.png")
                number := 3
                } else if (number = 3) { 
                CheckMap(z := "question.png")
                number := 4
                } else if (number = 4) {
                CheckMap(z := "cross.png")
                number := 1
                }
            } 
        }
    }
    
    CheckMap(x){  
        ImageSearch, foundX, foundY, 2000, 0, 2560, 1440, %x%
        if (ErrorLevel = 0) {
            MouseGetPos, StartX, StartY
            Click, %foundX%, %foundY%
            MouseMove, StartX, StartY
        }
    }
    
    *[::
    Suspend
    Pause, 1
    return
    
    *]::
    ExitApp
    return
    

    【讨论】:

    • 静态变量确实是一种解决方案。如果您想使用函数范围之外的变量,global 将是您正在寻找的。阅读有关变量范围的更多信息here
    猜你喜欢
    • 2022-11-21
    • 2013-11-03
    • 2014-10-06
    • 1970-01-01
    • 1970-01-01
    • 2021-09-18
    • 2023-03-07
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多