2014-02-27 124 views
-1

我遇到了代码循环问题。它只是运行结束。我可以编译它,当我这样做时,它会立即结束,永远不会循环。代码不会无限循环运行

我试图做的是有这样的代码神色一.bmp,然后如果发现点击不同的.bmp,所以它需要被扫描的.bmp所有的时间。

下面是代码:

#include <ImageSearch.au3> 

HotKeySet ("S" , "Start") 

$x = 0 
$y = 0 
$heal = 0 
$mp = 0 
$poth = 0 
$potm = 0 
$allon = 1 


Func Start() 
While 1 
    $Search = _ImageSearch('healthlow.bmp' , 0) 
    If $Search = 1 Then 
     $SearchPoth = _ImageSearch('poth.bmp' , 0, $x, $y, 0) 
     If $SearchPoth = 1 Then 
     MouseMove($x, $y, 10) 
     MouseClick("Left" , $x, $y, 10) 

     EndIf 
    EndIf 
    Sleep 30000 
    WEnd 
EndFunc 

我曾尝试:

While 1 = 1 

或进行变量始终是真实的。

回答

0

你永远不会打电话给你的功能Start()。仅仅声明它是不够的。您必须在主程序流程中至少明确地调用它一次。问题是,程序没有时间等待按下定义的热键。函数被定义,在此之后,程序刚刚结束,因为正常的程序流程(从第一行到最后一行)结束。你需要在你的函数定义之外加入另一个无限的Sleep-Loop,以等待一个HotKey输入... ;-)你可能并不是想把While循环放在你的函数中 - 或者你想要在按下S后每30秒钟定期搜索一次图像?

While True 
    Sleep(1) ; this could be much longer, doesn't really matter... 
WEnd 
+0

一旦启动程序,我希望它启动并每隔5秒继续扫描一次图像,并在关闭程序时仅停止扫描。 – Blink

+1

为什么你使用HotKey呢?你应该只使用一个在最后退出程序而无需点击托盘符号或终止进程... – Samoth

+0

然后只需删除'Func Start()'和'EndFunc'。 – Samoth

0

我有有我的代码循环的问题。它只是运行结束。我可以编译它,当我这样做时,它会立即结束,永远不会循环。

通过pressing Q退出While -loop(未经测试,没有错误检查):

Global Const $g_sKeyQuit = 'q' 
Global Const $g_iDelay  = 500 

Global  $g_bStateQuit = False 

Main() 

Func YourCode() 
    Local Static $iCount = 0 

    $iCount += 1 

    ConsoleWrite($iCount & @LF) 

EndFunc 

Func Main() 

    Start() 

    While Not $g_bStateQuit 

     YourCode() 
     Sleep($g_iDelay) 

    WEnd 

    Exit 

EndFunc 

Func SetStateQuit() 

    $g_bStateQuit = True 

EndFunc 

Func Start() 

    HotKeySet($g_sKeyQuit, 'SetStateQuit') 

EndFunc 

视觉说明(说明Main()While -loop):

Conditional While-loop

我想什么要做的是有这个代码寻找.bmp,然后如果发现点击不同的.bmp,所以它需要一直扫描.bmp

根据需要更换YourCode()的内容。 While -loops也可以是paused