通过函数传递数组然后循环

时间:2015-04-09 05:53:31

标签: autohotkey

我正在编写一个循环imagesearch的函数但我无法弄清楚如何使用Arrays允许的选项传递动态变量(例如Array0,它检索数组中记录的总数,以及Array%A_Index%当与循环一起使用时,显示每个名称,因为它通过列表)

arrowList = C:\AHK\LeftArrow.png|C:\AHK\LeftArrow1.png|C:\AHK\GreenLeftArrow.png
StringSplit, arrowArray, arrowList, |
buildList = C:\AHK\build1.png|C:\AHK\build2.png|C:\AHK\build3.png|C:\AHK\build4.png|C:\AHK\build5.png
StringSplit, buildArray, buildList, |


SearchArray("arrowArray","buildArray")


SearchArray(ByRef x, ByRef y) 
{
Loop, %x%
{
    x2get := %xA_Index%
    ImageSearch, imageX, imageY, 0, 0, A_ScreenWidth, A_ScreenHeight, *25 %x2get%
    tooltip, searching for %x2get% , 0, 0
    If ErrorLevel = 0
    {
        Loop, % y%0%
        {
            y2get := % y%A_Index%
            ImageSearch, imageX, imageY, 0, 0, A_ScreenWidth, A_ScreenHeight, *25 %y2get%
            tooltip, searching for %y2get% , 0, 0
            If ErrorLevel = 0
            {
                MouseClick, Left, imageX, imageY, 
                Sleep 1000
            }
        }
    }
}   
}

1 个答案:

答案 0 :(得分:3)

您在如何调用变量时遇到一些问题。你实际上并没有使用"真实"数组在那里,您正在使用名为"伪数组"的内容。您可以在文档here中了解它们。

他们是旧方式AHK处理数组,我强烈建议你尝试转移到在AHK中使用"real" arrays。如果您还没有 - http://ahkscript.org/download/,您还应该将您的AHK版本更新为最新版本。

我改变了脚本调用某些变量的方式,现在它应该可以工作了,试试这个,注意我已经评论了我改变的行:

arrowList = C:\AHK\LeftArrow.png|C:\AHK\LeftArrow1.png|C:\AHK\GreenLeftArrow.png
StringSplit, arrowArray, arrowList, |
buildList = C:\AHK\build1.png|C:\AHK\build2.png|C:\AHK\build3.png|C:\AHK\build4.png|C:\AHK\build5.png
StringSplit, buildArray, buildList, |

SearchArray("arrowArray", "buildArray")

SearchArray(ByRef x, ByRef y) 
{
    Loop, % %x%0 ; Changed this line
    {
        x2get := %x% A_Index ; Changed this line
        ImageSearch, imageX, imageY, 0, 0, %A_ScreenWidth%, %A_ScreenHeight%, *25 %x2get% ; Changed this line
        tooltip, searching for %x2get% , 0, 0
        If ErrorLevel = 0
        {
            Loop, % %y%0 ; Changed this line
            {
                y2get := % %y% A_Index ; Changed this line
                ImageSearch, imageX, imageY, 0, 0, %A_ScreenWidth%, %A_ScreenHeight%, *25 %y2get%  ; Changed this line
                tooltip, searching for %y2get% , 0, 0
                If ErrorLevel = 0
                {
                    MouseClick, Left, %imageX%, %imageY% ; Changed this line
                    Sleep 1000
                }
            }
        }
    }   
}

如果您对使用"真实"的解决方案感兴趣数组会看起来,这是一个例子。在尝试之前,请确保您运行的是最新版本的AHK,否则可能会失败。

arrowList := "C:\AHK\LeftArrow.png|C:\AHK\LeftArrow1.png|C:\AHK\GreenLeftArrow.png"
arrowArray := StrSplit(arrowList, "|")
buildList := "C:\AHK\build1.png|C:\AHK\build2.png|C:\AHK\build3.png|C:\AHK\build4.png|C:\AHK\build5.png"
buildArray := StrSplit(buildList, "|")

SearchArray(arrowArray, buildArray)

SearchArray(firstArray, secondArray) {

    ; Iterate through the first array
    for outerIndex, outerValue in firstArray {
        ; outerIndex = Index of the current element; 1, 2, etc...
        ; outerValue = The value of the string at that index

        Tooltip, Searching for %outerValue%, 0, 0
        ImageSearch, imageX, imageY, 0, 0, %A_ScreenWidth%, %A_ScreenHeight%, *25 %outerValue%
        if (ErrorLevel = 0) {

            ; Iterate through the second array
            for innerIndex, innerValue in secondArray {
                ; innerIndex = Index of the current element; 1, 2, etc...
                ; innerValue = The value of the string at that index

                Tooltip, Searching for %innerValue%, 0, 0
                ImageSearch, imageX, imageY, 0, 0, %A_ScreenWidth%, %A_ScreenHeight%, *25 %innerValue%
                if (ErrorLevel = 0) {
                    MouseClick, Left, %imageX%, %imageY%
                    Sleep, 1000
                }
            }
        }
    }
}