2014-02-27 187 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