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 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) 错误:函数不能包含函数。
程序现在将退出。
我不知道从哪里开始这个错误,我在其他论坛上读到说我的格式不正确。
解决方案
一些不同的事情:
- 后面的花括号
state := true
应该是另一种方式(}, not {) - AHK中没有默认
rand
函数,您可能正在寻找Random,或者您有一个名为rand
您的问题未显示的自定义函数。无论如何,我将编写一个函数,该函数rand(a,b)
将返回 a 和 b 之间的整数值
rand(a, b)
{
Random, rand, a, b
return rand
}
- 此外,
getState()
在loop 10000
. 我不确定它应该做什么(或者如果您的意思是GetKeyState之类的东西),但我假设您已经涵盖了这一点。 - 正如@Pranav Hosangadi 提到的,您可能想要一个
else if
声明,而不仅仅是else
这一行的声明:else(gemColor = 96885A)
- 你确定要
SendMode Input
吗?尽管它的速度确实比 standard 更快Send
,但它的用途通常仅限于在文本框中输入文本。您似乎正在尝试向 Flash 游戏发送按键,因此您可能需要检查该按键是否按您的预期运行。 - 在编写结束大括号 ( }) 来结束
if()
orelse()
子句时,您需要将其放在单独的行中。(即改变
if(state = true)
{Mine()}
else
{Sleep, rand(70,150)}
类似于
if(state = true)
{
Mine()
}
else
{
Sleep, rand(70,150)
}
甚至(因为这里的if
andelse
语句每个只触发一行代码)
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 如果某些东西不能正常工作,我会尝试更新这个响应
推荐阅读
- observable - observe changes in a different viewmodel
- google-apps-script - Unable to set freeze row in spreadsheet
- linux - 如果文件不存在,则毫无例外地从 GCS 中删除
- c++ - IloBoolVarArray syntax help needed
- angular - Is there a way to automatically get to the bottom of a virtual scroll?
- haskell - 如何将具有 2 个值的向量拆箱
- android - 两个 EditText 视图的双向数据绑定
- html - 在多个表格中拆分 HTML 表格
- .htaccess - 如果文件不存在,则重定向到文件并保留 url
- keycloak - 根据身份提供者令牌中存在的数据更新 Keycloak 用户数据