Android Question Problem with buttons and stages in LIBGDX.

Eme Fibonacci

Well-Known Member
Licensed User
Hi,
I have two stages. Each stage has a button.

If I initialize in this order:

Stage1.Initialize ("Stage1")
Stage2.Initialize ("Stage2")

Only the stage 2 button works.

If I initialize in this order:

Stage2.Initialize ("Stage2")
Stage1.Initialize ("Stage1")

Only the stage 1 button works.

Here is a sample code (use key back to toggle between stages).

B4X:
Sub Process_Globals
End Sub

Sub Globals
    Public lGdx As LibGDX
    Private GL As lgGL
    Private LGSM As lgScreenManager
    Private LG_Screen(2) As lgScreen
    Private lastscreen As Int
    Private Stage1, Stage2 As lgScn2DStage
End Sub

Sub Activity_Create(FirstTime As Boolean)
    Private V As View = lGdx.InitializeView("LG")
    Activity.AddView(V, 0,0,100%x,100%y)
End Sub

Sub Activity_Resume
End Sub

Sub Activity_Pause (UserClosed As Boolean)
End Sub

Sub LG_Create
    Stage2.Initialize("Stage2")
    Stage1.Initialize("Stage1")
 
    CreateStage1
    CreateStage2
    LGSM.Initialize(lGdx)
    LG_Screen(0) = LGSM.AddScreen("LGS1")
    LG_Screen(1) = LGSM.AddScreen("LGS2")
    LGSM.CurrentScreen = LG_Screen(lastscreen)
End Sub

Sub Activity_KeyPress (KeyCode As Int) As Boolean 'Return True to consume the event
    If lastscreen=0 Then
        lastscreen=1
        LGSM.CurrentScreen=LG_Screen(1)
        Return True
    End If
    lastscreen=0
    LGSM.CurrentScreen=LG_Screen(0)
    Return True
End Sub

Sub LGS1_Show
    lastscreen=0
End Sub
Sub LGS1_Resize(Width As Int, Height As Int)
    Stage1.SetViewport(480,854,False)
End Sub
Sub LGS1_Render(Delta As Float)
    GL.glClear(GL.GL10_COLOR_BUFFER_BIT)
    Stage1.SpriteBatch.ProjectionMatrix=Stage1.Camera.Combined
    Stage1.Draw
    Stage1.Act
End Sub

Sub LGS2_Show
    lastscreen=1
End Sub

Sub LGS2_Resize(Width As Int, Height As Int)
    Stage2.SetViewport(480,854,False)
End Sub
Sub LGS2_Render(Delta As Float)
    GL.glClear(GL.GL10_COLOR_BUFFER_BIT)
    Stage2.SpriteBatch.ProjectionMatrix=Stage2.Camera.Combined
    Stage2.Draw
    Stage2.Act
End Sub

Sub CreateStage1
    Dim b1 As lgScn2DButton
    Dim texb1,texb2 As lgTexture
    texb1.Initialize("normal.png") : texb2.Initialize("pressed.png")
    Private npBtnUp As lgScn2DSpriteDrawable :    npBtnUp.Initialize4(texb1)
    Private npBtnDown As lgScn2DSpriteDrawable : npBtnDown.Initialize4(texb2)
    Private stylButton As lgScn2DButtonStyle :    stylButton.Initialize(npBtnUp,npBtnDown)
    stylButton.SetDrawableSize(100,100) :    b1.Initialize(stylButton,"BTN")
    b1.Name="btn1" : b1.X=100 :    b1.Y=100 :    Stage1.AddActor(b1)
End Sub
Sub CreateStage2
    Dim b2 As lgScn2DButton
    Dim texb1,texb2 As lgTexture
    texb1.Initialize("normal.png") : texb2.Initialize("pressed.png")
    Private npBtnUp As lgScn2DSpriteDrawable :    npBtnUp.Initialize4(texb1)
    Private npBtnDown As lgScn2DSpriteDrawable : npBtnDown.Initialize4(texb2)
    Private stylButton As lgScn2DButtonStyle :    stylButton.Initialize(npBtnUp,npBtnDown)
    stylButton.SetDrawableSize(100,100) :    b2.Initialize(stylButton,"BTN")
    b2.Name="btn2" : b2.X=200 :    b2.Y=200 :    Stage2.AddActor(b2)
End Sub

Sub BTN_Click(CheckedState As Boolean)
    Private btn As lgScn2DButton = Sender
    Log(btn.Name)
End Sub
Any idea?

Thank you.

P.S.
If you can test my code, please copy the images to the file folder.
 

Attachments

Last edited:
Top