Basically, after the text I fade in (and later fade out) asynchronly I want to fade in and out another text (like "ready", "set", "go"). As fade in and out are/will be async I don't know when the fade out process is ready. So I want to use a callback which calls the same fade in/out function again like 'p_showTextAsync("Ready") and then "p'showTextAsync("Set")' and so on.
I made a small example (without the parameters) which shows the problem: The first time p_showTextAsync() is called everything works fine, but when this function is called again via the callback, in the first loop I get this error when WaitEvent() is called: "Wrong usage/parameters for this command!"
I don't know why it works in the first call but not when called second!?
Here is the code:
Code: Select all
timeoutID = Nil
counter = 0
Function p_DummyForTextFading()
; (unfortunately) needed for setInterval. Otherwise the frame printing loop would not continue
EndFunction
Function p_main()
InstallEventHandler({OnKeyUp = p_EventFunc})
SetInterval(2, p_DummyForTextFading, 1000/25) ; needed, so WaitEvent after AsyncDrawFrame() call continues
Local myCallback = {["function"] = p_showTextAsync}
p_showTextAsync(myCallback )
EndFunction
Function p_showTextAsync(callback)
DebugPrint("counter: " .. counter)
For Local k = 1 To 20
;do async stuff like AsyncDrawFrame(textObject , k) ; draw frame by frame
DebugPrint("k: " .. counter .. " - " .. k)
; VWait or the like are not suitable to use in this case as it blocks the script so no (keypress) event can be handled while anim
WaitEvent() ; needed, so the loop continues (through SetInterval) and listens to keypress events (InstallEventHandler)
; throws error when p_showTextAsync() is called the second time
Next
timeoutID = SetTimeout(Nil, p_FadeTextOut, 1000, callback) ; begin to fade out the text after 5 seconds; TODO: make async
EndFunction
Function p_FadeTextOut(message)
timeoutID = Nil
counter = counter + 1
If (HaveItem(message, "userData"))
message.userData["function"]() ; execute callback after the fade out process has finished
EndIf
EndFunction
p_main() ; app entry
While quit = False
WaitEvent
Wend