问题描述
我是 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
函数,您可能正在寻找 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
吗?尽管它确实比标准的Send
速度更快,但它的使用通常仅限于在文本框中输入文本。您似乎正在尝试向 Flash 游戏发送击键,因此您可能需要检查该游戏是否按您的预期运行。 - 写花括号 (}) 来结束
if()
或else()
子句时,您需要将它放在自己的行上。 (即改变
if(state = true)
{Mine()}
else
{Sleep,rand(70,150)}
类似的东西
if(state = true)
{
Mine()
}
else
{
Sleep,150)
}
甚至(因为这里的 if
和 else
语句只触发一行代码)
if(state = true)
Mine()
else
Sleep,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
return rand
}
^-::
loop 10000
{
;getState()
if(state = true)
Mine()
else
Sleep,150)
}
lmk 如果出现问题,我会尝试更新此回复