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

Passing an Array through Function Then Looping

我正在编写一个循环图像搜索的函数,但我无法弄清楚如何使用数组允许的选项传递动态变量(例如检索数组中记录总数的 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
            }
        }
    }
}   
}

您在调用变量的方式上遇到了一些问题。您实际上并没有在那里使用 "real" 数组,您使用的是所谓的 "pseudo-arrays"。您可以在文档 here.

中阅读有关它们的信息

它们是 AHK 处理数组的旧方法,我强烈建议您尝试改用 "real" arrays in AHK. You should also update your version of AHK to the latest if you haven't already - http://ahkscript.org/download/

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

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
                }
            }
        }
    }   
}

如果您对使用 "real" 数组的解决方案的外观感兴趣,这里有一个例子。在尝试之前,请确保您是 运行 最新版本的 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
                }
            }
        }
    }
}