首页 > 解决方案 > 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 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 行出错。

Line Text else(gemColor = 96885A) 错误:函数不能包含函数。

程序现在将退出。

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

标签: autohotkey

解决方案


一些不同的事情:

  1. 后面的花括号state := true应该是另一种方式(}, not {
  2. AHK中没有默认rand函数,您可能正在寻找Random,或者您有一个名为rand您的问题未显示的自定义函数。无论如何,我将编写一个函数,该函数rand(a,b)将返回 a 和 b 之间的整数值
rand(a, b)
{
    Random, rand, a, b
    return rand
}
  1. 此外,getState()loop 10000. 我不确定它应该做什么(或者如果您的意思是GetKeyState之类的东西),但我假设您已经涵盖了这一点。
  2. 正如@Pranav Hosangadi 提到的,您可能想要一个else if声明,而不仅仅是else这一行的声明:else(gemColor = 96885A)
  3. 你确定要SendMode Input吗?尽管它的速度确实比 standard 更快Send,但它的用途通常仅限于在文本框中输入文本。您似乎正在尝试向 Flash 游戏发送按键,因此您可能需要检查该按键是否按您的预期运行。
  4. 在编写结束大括号 ( }) 来结束if()orelse()子句时,您需要将其放在单独的行中。(即改变
    if(state = true) 
        {Mine()}
    else
        {Sleep, rand(70,150)}

类似于

    if(state = true) 
        {
            Mine()
        }
    else
        {
            Sleep, rand(70,150)
        }

甚至(因为这里的ifandelse语句每个只触发一行代码)

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

lmk 如果某些东西不能正常工作,我会尝试更新这个响应


推荐阅读