Android Question timer in class associated with wrong object

sterlingy

Active Member
Licensed User
Longtime User
I'm creating a objects by calling a class from my Main module. In the class, I start a timer with a 3 second interval. During the timer's Tick sub, I set a flag to destroy this object during an update of the Main module.

The problem is that it is not destroying the object with which the timer is associated. Instead, it is destroying the last object created.

Any help would be appreciated.

Cheers,

Sterling

------------------------------------

Here is the class, truncated:

B4X:
'Class module
Sub Class_Globals
    Dim bodyMath As lgBox2DBody
    Dim mathSprite As lgSprite
    Dim UD As imageType
    Dim killBody As Boolean = False
   
    Dim lifeTimer As Timer
   
End Sub

'Initializes the object. You can add parameters to this method if needed.
Public Sub Initialize(vTexture As lgSprite, vWidth As Float, vHeight As Float, vPosX As Int, vPosY As Int, vbodyType As String, vReset As Boolean, vAngle As Float, vKill As Boolean)

    lifeTimer.Initialize("LifeTimer", 3000)

    UD.mathItem = vbodyType
    UD.killObj = vKill
   
.
.
.
   
    lifeTimer.Enabled = True
   
End Sub

Private Sub lifeTimer_Tick

    UD.killObj = True

    lifeTimer.Enabled = False

End Sub

And, in the Main Module, here is the code that goes through a LIST of objects and if one has the "kill" flag set to TRUE, it is destroyed:

B4X:
    Dim bodyArray As lgArray
    bodyArray.Initialize
    World.GetAllBodies(bodyArray)
    Dim tmpX, tmpY As Float
    Dim i As Int
    Dim tmpBody As lgBox2DBody
    Dim tmpUserD As imageType

    For i = 0 To bodyArray.Size - 1
        tmpBody = bodyArray.Get(i)
        tmpUserD = tmpBody.UserData
        If tmpUserD.mathItem <> "bullet" Then

.
.
.
           
            If tmpUserD.mathItem.Contains("math") AND tmpUserD.killObj = True Then
                lstNuke.Add(tmpBody)
            End If
        End If
    Next
   
    'Destroys the bodies
    For i = 0 To lstNuke.Size - 1
        Dim b As lgBox2DBody = lstNuke.Get(i)
        World.DestroyBody(b)
    Next
 
Top