是否可以组合pixelsearch和pixelgetcolor

时间:2015-05-18 23:02:19

标签: autohotkey

在这个MMO游戏中,如果我的角色HP低,它应该传送回城镇并与治疗NPC交谈。它无法正常工作,我无法弄清楚原因 - 当我尝试运行脚本时出现错误。

http://i61.tinypic.com/swqltw.jpg

CoordMode, Pixel, Relative
CoordMode, Mouse, Relative
Home:: ;press home to start ahk


Loop   

PixelSearch, X, Y, 0, 0, %A_ScreenWidth%, %A_ScreenHeight%, 0x00ff00, 0, fast ;<<<--this is the monster color to attack
if(ErrorLevel=0) 
Sleep, 200
Send, {F4}
Sleep, 200
MouseClick, left, 392, 289
Sleep, 8000


else 

   PixelGetColor, healthbar, 51, 82 ;heres my health bar coordinates

  if (healthbar = 0xDEDED6) ;if my hp bar goes low it will go teleport to town and click to healer npc
       Send !3 ;alt 3 to teleport to town
       Sleep 5000 ;lets wait 5seconds for waiting game loading.
       MouseClick, left, 755, 341 ;click the healer npc
       sleep, 1500
       Send {F5} ;some self skill buffs and stuffs
       sleep, 1500
       Send {F5}
       sleep, 1500
       MouseClick, left, 755, 341   ;click to healer npc again to heal mana
       sleep, 500
       MouseClick, left, 713, 281 ;click to teleporter npc to get back to dungeon to kill monsters
       sleep, 500
       Send {Enter}
       sleep, 1500
       Send {Enter}
else 
       Send {F8} ;if no monster present in screen, use random teleport skill to search for monsters


F11::Pause
End::ExitApp ; alt+x to exit ahk

1 个答案:

答案 0 :(得分:3)

在所有非低级编程语言中,您缺少一个非常重要的编程部分:大括号。 (在AutoIt中,我认为你不使用大括号,而是ifendif / wend等等,而不是基本相同

重要的是要了解AutoHotkey中的编译器不关心脚本中的缩进。

快速举例:

if(healthbar = 0xDEDED6)
send, !3
sleep, 5000

相同
if(healthbar = 0xDEDED6)
{
    send !3
}
sleep, 5000

。因此,sleep, 5000将以任何方式执行,无论if语句如何!如果您根本不使用任何大括号,则任何if - 语句,loopelse部分等只会计入一行。所以,永远不要忽略大括号,只要只执行一个行。

如果您想确定,请尽可能使用大括号。

另一件事:如果您使用home::之类的热键触发器启动任何部分,请务必最后用return将其括起来。

以下是您的源代码示例。我也改变了一些缩进,但要注意它们不是必需的,只是为了更好的可读性。

CoordMode, Pixel, Relative
CoordMode, Mouse, Relative

Home:: ;press home to start ahk

Loop
{

    PixelSearch, X, Y, 0, 0, %A_ScreenWidth%, %A_ScreenHeight%, 0x00ff00, 0, fast ;<<<--this is the monster color to attack
    if(ErrorLevel=0)
    {
        Sleep, 200
        Send, {F4}
        Sleep, 200
        MouseClick, left, 392, 289
        Sleep, 8000
    }
    else
    {
        PixelGetColor, healthbar, 51, 82 ;heres my health bar coordinates
        if (healthbar = 0xDEDED6) ;if my hp bar goes low it will go teleport to town and click to healer npc
        {
           Send !3 ;alt 3 to teleport to town
           Sleep 5000 ;lets wait 5seconds for waiting game loading.
           MouseClick, left, 755, 341 ;click the healer npc
           sleep, 1500
           Send {F5} ;some self skill buffs and stuffs
           sleep, 1500
           Send {F5}
           sleep, 1500
           MouseClick, left, 755, 341   ;click to healer npc again to heal mana
           sleep, 500
           MouseClick, left, 713, 281 ;click to teleporter npc to get back to dungeon to kill monsters
           sleep, 500
           Send {Enter}
           sleep, 1500
           Send {Enter}
        }
        else 
           Send {F8} ;if no monster present in screen, use random teleport skill to search for monsters
    }

}

return  ; ------ end of HOME hotkey


F11::Pause
End::ExitApp ; alt+x to exit ahk

但是,这并不能保证您的代码现在可以正常工作