Other Quiz #12 - Does it work?

Erel

B4X founder
Staff member
Licensed User
Longtime User
B4X:
Sub Class_Globals
    Private Root As B4XView
    Private xui As XUI
    Private LastClickedTime As Long
End Sub

Public Sub Initialize
End Sub

Private Sub B4XPage_Created (Root1 As B4XView)
    Root = Root1
    Root.LoadLayout("MainPage")
End Sub

Sub Button1_Click
    Dim MillisecondsPassedSinceLastClick As Int = DateTime.Now - LastClickedTime
    If MillisecondsPassedSinceLastClick < 2000 Then
        Log("ignore")
    Else
        LastClickedTime = DateTime.Now
        Log("Do something important that shouldn't run if less than 2 seconds passed since last click")        
    End If
End Sub

Quite simple but I fell for this one myself...
 

JohnC

Expert
Licensed User
Longtime User
LastClickedTime is not initially set a value of DateTime.Now
 
Upvote 0

JohnC

Expert
Licensed User
Longtime User
You should have used Long for MillisecondsPassedSinceLastClick because it will overflow on the first run because LastClickedTime is initialy set to "0" by default, so it would try to stuff the full long value of DateTime.Now into an Integer.
 
Last edited:
Upvote 0

JohnC

Expert
Licensed User
Longtime User
Fun Fact:
If you had a time machine and went back to 1970 - the above code would work because then the value of DateTime.Now would be low enough to fit into an Integer
 
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
As it was solved very quickly and I want to entertain Luca a bit:

Kvu0JofJxQ.gif

It looks much better than in the above gif (run in release mode).

B4X:
Private Sub B4XPage_Created (Root1 As B4XView)
    Root = Root1
    Root.LoadLayout("MainPage")
    For i = 80 To 5 Step - 1
        Dim pnl As B4XView = xui.CreatePanel("")
        pnl.SetColorAndBorder(Rnd(0x8fffffff, 0), 1dip, xui.Color_Black, 5dip)
        pnl.Tag = i
        Pane1.AddView(pnl, 0, 0, 50dip, 50dip)
    Next
    Do While True
        Dim x As Int = TargetX
        Dim y As Int = TargetY
        For Each pnl As B4XView In Pane1.GetAllViewsRecursive
            Dim order As Int = pnl.Tag
            Dim dy As Float = y - pnl.Top
            Dim dx As Float = x - pnl.Left
            Dim alpha As Float = ATan2D(dy, dx)
            Dim dist As Float = Sqrt(Power(dy, 2) + Power(dx, 2))
            Dim speed As Float = Min(order, dist)
            pnl.Left = pnl.Left + CosD(alpha) * speed
            pnl.Top = pnl.Top + SinD(alpha) * speed
            x = pnl.Left
            y = pnl.Top
        Next
        Sleep(30)
    Loop
End Sub

Private Sub Pane1_Touch (Action As Int, X As Float, Y As Float)
    TargetX = X
    TargetY = Y
End Sub
 

Attachments

  • 1.zip
    2.5 KB · Views: 182
Upvote 0
Top