B4J Question Timer Tick

Clybawn

New Member
Hi everyone,

I'm using B4J for the first time and im struggling a lot.

I have to implement a programm for traffic lights based on a tcp/ip connection.
I managed to do the connection thing.

Now I want to program the traffic light. I want to use a simple timer.

This is what i've got so far:

dim Timer as Timer
.
.
.


Sub Ampel

Label1.style="-fx-background-color: green"
Timer.Initialize("Timer1",3000)
Timer.Initialize("Timer2",1000)
Timer.Enabled = True

End Sub
Sub Timer1_tick

Label1.style="-fx-background-color: red"
Log("test")


End Sub

It works untill i set the color of my label to green. The action from the timer1_tick never starts.
Why? :/

Thank you very much for your help!
 

klaus

Expert
Licensed User
Longtime User
You need to define two distinct Timers!
B4X:
Dim xui As XUI ' jXUI library
Dim Timer1, Timer2 As Timer
Dim Label1 As B4XView
And then:
B4X:
Sub Ampel
    Label1.Color = xui.Color_Green
    Timer1.Initialize("Timer1",3000)
    Timer2.Initialize("Timer2",1000)
    Timer2.Enabled = True
End Sub

Sub Timer1_Tick
    Label1.Color = xui.Color_Red
    Log("Tick1")
    Timer2.Enabled = False
    Timer1.Enabled = True
End Sub

Sub Timer2_Tick
    Label1.Color = xui.Color_Green
    Log("Tick2")
    Timer1.Enabled = False
    Timer2.Enabled = True
End Sub

You probably need to modify the timmer intervals.

In your code, you define only one Timer and in these two lines:
B4X:
Timer.Initialize("Timer1",3000)
Timer.Initialize("Timer2",1000)
the second overwrites the previous one.
 
Upvote 0
Cookies are required to use this site. You must accept them to continue using the site. Learn more…