Autohotkey 自动热键-函数不能包含函数错误

Autohotkey 自动热键-函数不能包含函数错误,autohotkey,Autohotkey,我不熟悉AutoHotKey,我想为flash游戏创建一个脚本宏,但当我运行它时,它会产生一个错误 #NoEnv ; Recommended for performance and compatibility with future AutoHotkey releases. ; #Warn ; Enable warnings to assist with detecting common errors. SendMode Input ; Recommended for new script

我不熟悉AutoHotKey,我想为flash游戏创建一个脚本宏,但当我运行它时,它会产生一个错误

#NoEnv  ; Recommended for performance and compatibility with future AutoHotkey releases.
; #Warn  ; Enable warnings to assist with detecting common errors.
SendMode Input  ; Recommended for new scripts due to its superior speed and reliability.
SetWorkingDir %A_ScriptDir%  ; Ensures a consistent starting directory.

condChecker := false
state := true

Mine() 
{
    Sleep, rand(10,80)
    Send, {Space}
    while(state = true) 
        {
                PixelGetColor, gemColor, 982, 433
        if(gemColor = B93557) 
        {
            state := true
        {
        else(gemColor = 96885A) 
        {
            state := false
        }
                Sleep, rand(90,120)
        }
}

^-::
loop 10000 
{
    getState()
    if(state = true) 
        {Mine()}
    else
        {Sleep, rand(70,150)}
}
当我在ahk文件上按Run Script时,会弹出一个菜单说

第20行出错

行文字else(gemColor=96885A) 错误:函数不能包含函数

程序现在将退出


我不知道从哪里开始这个错误,我在其他论坛上读到说我的格式不正确。

一些不同的事情:

  • state:=true之后的大括号应该是另一种方式(},而不是{)
  • AHK中没有默认的
    rand
    函数,您可能正在寻找,或者您有一个名为
    rand
    的自定义函数,您的问题中没有显示该函数。在任何情况下,我将编写一个函数
    rand(a,b)
    ,该函数将返回a和b之间的整数值
  • 此外,还有另一个函数
    getState()
    正在
    循环10000
    中调用。我不确定它应该做什么(或者如果您是指类似的功能),但我假设您已经讨论过了
  • 正如@Pranav Hosangadi所提到的,您可能需要一个
    else if
    语句,而不是这一行的
    else
    语句:
    else(gemColor=96885A)
  • 是否确实要
    发送模式输入
    ?尽管它的速度确实比标准的
    发送
    要快,但它的使用通常仅限于在文本框中键入文本。您似乎正在尝试向flash游戏发送击键,因此您可能需要检查它是否按预期运行
  • 在编写结束大括号(})以结束
    if()
    else()
    子句时,需要将其放在自己的行中。(即变化)
  • 差不多

        if(state = true) 
            {
                Mine()
            }
        else
            {
                Sleep, rand(70,150)
            }
    
    甚至(因为这里的
    if
    else
    语句只触发一行代码)


    这有点长,但最后的代码是:

    #NoEnv  ; Recommended for performance and compatibility with future AutoHotkey releases.
    ; #Warn  ; Enable warnings to assist with detecting common errors.
    ; ---> Double check this! ---> SendMode Input
    SetWorkingDir %A_ScriptDir%  ; Ensures a consistent starting directory.
    
    condChecker := false
    state := true
    
    Mine() 
    {
        Sleep, rand(10,80)
        Send, {Space}
        while(state = true) 
            {
                    PixelGetColor, gemColor, 982, 433
            if(gemColor = B93557) 
            {
                state := true
            }
            else if(gemColor = 96885A) 
            {
                state := false
            }
                    Sleep, rand(90,120)
        }
    }
    
    rand(a, b)
    {
        Random, rand, a, b
        return rand
    }
    
    ^-::
    loop 10000 
    {
        ;getState()
        if(state = true) 
            Mine()
        else
            Sleep, rand(70,150)
    }
    

    如果某些东西不能正常工作,我将尝试更新此响应

    您是否打算编写<代码>否则如果(…)
    ?感谢您的快速响应。代码运行了,但我的逻辑似乎有一个错误,我不明白。在游戏中,有一块宝石将位于我的角色上方。当一块石头中有一块宝石时,当它有一块红色宝石从中探出时,您就会知道它。当您按下空格键时,它将自动直到宝石出现为止宝石被开采。开采将继续,直到宝石被开采或玩家再次按下空格。当红色宝石的区域变成棕色时,你就会知道宝石被开采完毕。游戏是2D口袋妖怪flash游戏,所以宝石位置将始终保持在同一个位置。当我运行当前程序时,它只是垃圾压榨空格键。我看了我的代码后也不明白为什么会出现这种情况。如果我知道如何发送屏幕截图,我会,但我甚至不知道如何回复答案,所以我在这里。
        if(state = true) 
            {
                Mine()
            }
        else
            {
                Sleep, rand(70,150)
            }
    
        if(state = true) 
            Mine()
        else
            Sleep, rand(70,150)
    
    #NoEnv  ; Recommended for performance and compatibility with future AutoHotkey releases.
    ; #Warn  ; Enable warnings to assist with detecting common errors.
    ; ---> Double check this! ---> SendMode Input
    SetWorkingDir %A_ScriptDir%  ; Ensures a consistent starting directory.
    
    condChecker := false
    state := true
    
    Mine() 
    {
        Sleep, rand(10,80)
        Send, {Space}
        while(state = true) 
            {
                    PixelGetColor, gemColor, 982, 433
            if(gemColor = B93557) 
            {
                state := true
            }
            else if(gemColor = 96885A) 
            {
                state := false
            }
                    Sleep, rand(90,120)
        }
    }
    
    rand(a, b)
    {
        Random, rand, a, b
        return rand
    }
    
    ^-::
    loop 10000 
    {
        ;getState()
        if(state = true) 
            Mine()
        else
            Sleep, rand(70,150)
    }