Android Question Wait For inside Loop ??

ilan

Expert
Licensed User
Longtime User
hi

i am trying to use a wait for inside a loop but i get an error.
i am doing it inside a Do...While loop. using sleep() does work but using a wait for() not.
am i doing something wrong or its not possible to do that like that?

relevant code:

B4X:
'...
Wait For(drawsmoohtplayer) Complete (finished As Boolean)
    If finished Then
        If keepgoing Then
            ismoving = True
             trykeepgoing(direction)                     
       End If
   End If
'...


Sub drawsmoohtplayer As ResumableSub
    Dim lastcell As cell = grid.Get(lastcellpos)
    Dim cell As cell = grid.Get(myPlayer.pPos)
    Dim difx As Float = (cell.left - lastcell.left) / 6
    Dim dify As Float = (cell.top - lastcell.top) / 6
    For i = 1 To 6 '6 frames
        cONMAZE.ClearRect(cONMAZE.TargetRect)
        Dim r As B4XRect
        r.Initialize(lastcell.left+(w*0.22)+xCenter+(difx*i),lastcell.top+(w*0.22)+(dify*i),lastcell.right-(w*0.22)+xCenter+(difx*i),lastcell.bottom-(w*0.22)+(dify*i))
        cONMAZE.DrawRect(r,xui.Color_White,True,0)
        cONMAZE.Invalidate
        Sleep(20)
    Next
    Return True
End Sub

error:
 

Erel

B4X founder
Staff member
Licensed User
Longtime User
So, I think your Sleep(20) inside the loop maybe is returning "finished" as null.
It doesn't return null because he correctly calls drawsmoothplayer with Wait For.

This code should work.
Similar example:
B4X:
Sub Activity_Create(FirstTime As Boolean)
   Log("before")
   Wait For (Test) Complete (Finish As Boolean)
   Log("after")
End Sub

Sub Test As ResumableSub
   For i = 1 To 6
       Log(i)
       Sleep(500)
   Next
   Return True
End Sub
 
Upvote 0

ilan

Expert
Licensed User
Longtime User

I dont see any difference between your code and mine erel. Am i missing something?
 
Upvote 0

OliverA

Expert
Licensed User
Longtime User
What is calling the code that has the wait for?
 
Upvote 0

ilan

Expert
Licensed User
Longtime User
No. I expect your code to work. The problem is somewhere else...

i have changed the structure of the code and it is working now but what i did is i have put the wait for in a do while loop and i got those errors. now i dont do it like this
i create a list and add all vectors to the list in the do while loop and then i call the animation and this is working well.

the weired thing is that i tried to reproduce the error i got yesterday in b4j but it is working. could it be that it is a bug in b4a? i dont have b4a at work so i cannot test it now. this is the code that should raise those errors

B4X:
#Region Project Attributes
    #MainFormWidth: 600
    #MainFormHeight: 600
#End Region

Sub Process_Globals
    Private fx As JFX
    Private MainForm As Form
End Sub

Sub AppStart (Form1 As Form, Args() As String)
    MainForm = Form1
    'MainForm.RootPane.LoadLayout("Layout1") 'Load the layout file.
    MainForm.Show
   
    Dim nr As Int = 0
   
    Do While nr < 5
        nr = nr + 1
        Log("before wait for")
        Wait For (test) Complete (fin As Boolean)
        Log("after wait for")
    Loop
   
    Log("end of test")
   
End Sub

Sub test As ResumableSub
    Log("test was called")
    Sleep(50)
    Return True
End Sub

'Return true to allow the default exceptions handler to handle the uncaught exception.
Sub Application_Error (Error As Exception, StackTrace As String) As Boolean
    Return True
End Sub
 
Upvote 0

MarkusR

Well-Known Member
Licensed User
Longtime User
was i am missing in
drawsmoohtplayer is
Dim i as int
and
w as sub input parameter.
grid and cONMAZE can also be a functional parameter
 
Upvote 0
Cookies are required to use this site. You must accept them to continue using the site. Learn more…