Android Question libGDX Please Help

abhishek007p

Active Member
Licensed User
Longtime User
hello! Its me again im not sure if anyone can help but here is my code:

B4X:
Sub MovePlayer
    If TouchPos.x <> Position.x AND TouchPos.y <> Position.y Then
       
        If TouchPos.x > Position.x Then
            'move right
            Position.x = (Position.x + 200 * LibGDX.Graphics.DeltaTime)
        Else If TouchPos.x < Position.x Then
            'move left
            Position.x = (Position.x - 200 * LibGDX.Graphics.DeltaTime)
        Else
            Position.x = TouchPos.x - bgBalloon.Width / 2
            TouchPos.x = Position.x
        End If

        If TouchPos.y > Position.y Then
            'move up
            Position.y = (Position.y + 200 * LibGDX.Graphics.DeltaTime)
        Else If TouchPos.y < Position.y Then
            'move down
            Position.y = (Position.y - 200 * LibGDX.Graphics.DeltaTime)
        Else
            Position.y = TouchPos.y - bgBalloon.Height / 2
            TouchPos.y = Position.y
        End If
       
    End If
End Sub

Sub InputProc_TouchDown(ScreenX As Int, ScreenY As Int, Pointer As Int) As Boolean
   'transform x and y location
   TouchPos.x = vpWidth * (ScreenX / LibGDX.Graphics.Width)
   TouchPos.y = vpHeight - (vpHeight * (ScreenY / LibGDX.Graphics.Height)) 'flip Y coords
   Log("TouchPos.x: " & TouchPos.x)
   Log("TouchPos.y: " & TouchPos.y)
   Return False
End Sub

You can also download the attached project file.

what im trying to do is move the balloon to where the screen was last touched.

Can anyone help me on how will i fix it? the only problem is the logic on how it will move smoothly.

@Erel , @Informatix, @anyone who knows libgdx
 

Attachments

  • libgdx.zip
    476.3 KB · Views: 202

abhishek007p

Active Member
Licensed User
Longtime User
If you want to display a dialog, you have to create it. A MsgBox will crash your app and would look a bit strange in a game. The simplest way to create a dialog is to use a Window (a Scene2D container).

ohh.. okay, then its not possible to add the native b4a UI like msgbox and popup dialogs? because i just saw a game made with libgdx uses popup dialog with ads and buttons on it.
 
Upvote 0

abhishek007p

Active Member
Licensed User
Longtime User
@Informatix , i have this code,

B4X:
Sub lTouchControl_KeyDown(KeyCode As Int) As Boolean
    If KeyCode = KeyCodes.KEYCODE_BACK Then
        If GameState = GameState_STARTED OR GameState = GameState_GAMEOVER Then
            GameState = GameState_HOME 'go back to main game screen (menu)
            Return True
        End If
    End If
    Return False
End Sub

if i press 'BACK' button, my game state will change to GameState_HOME (which is correct) but after that the game will also quit which shouldn't be, Return True should consume the event right?..
 
Upvote 0

Informatix

Expert
Licensed User
Longtime User
@Informatix , i have this code,

B4X:
Sub lTouchControl_KeyDown(KeyCode As Int) As Boolean
    If KeyCode = KeyCodes.KEYCODE_BACK Then
        If GameState = GameState_STARTED OR GameState = GameState_GAMEOVER Then
            GameState = GameState_HOME 'go back to main game screen (menu)
            Return True
        End If
    End If
    Return False
End Sub

if i press 'BACK' button, my game state will change to GameState_HOME (which is correct) but after that the game will also quit which shouldn't be, Return True should consume the event right?..
You can intercept and block the back key by adding lGdx.Input.CatchBackKey = True to LG_Create.
 
Upvote 0

abhishek007p

Active Member
Licensed User
Longtime User
@Informatix and everyone, i really need your help in fixing this player movement. please see the attached zip.

CTRL+I and search for TODO to see the code that needs fixing.

How can i make it move in a straight/diagonal line?..
 

Attachments

  • diamondhunter.zip
    8.2 KB · Views: 165
Upvote 0

abhishek007p

Active Member
Licensed User
Longtime User
You can intercept and block the back key by adding lGdx.Input.CatchBackKey = True to LG_Create.

Just bumping my thread and here's an additional method on catching back key without using this code 'lGdx.Input.CatchBackKey=true'

B4X:
Sub Activity_KeyPress (KeyCode As Int) As Boolean 'Return True to consume the event
    If GameState = GameState_Playing Then
        If KeyCode = KeyCodes.KEYCODE_BACK Then
            If lGdx.IsInitialized Then lGdx.Pause
            If Msgbox2("Return to main menu?", "", "Yes", "", "No", Null) = DialogResponse.POSITIVE Then
                GameState = GameState_Menu                
            End If
            If lGdx.IsInitialized Then lGdx.Resume
            Return True
        End If
    End If
    Return False
End Sub

will just create another game since i still dont have a fix for my last problem.
 
Upvote 0

abhishek007p

Active Member
Licensed User
Longtime User
im bumping this thread again as i figure out a solution on my star daze clone.

Using Scene2d stage and actor solved my problem. The action MoveTo2(X,Y,Duration) is the function im looking for, but then again, i will need to learn more about scene2d.

the only question i have so far, since im using Scene2d Stage then, how should i draw my background?.. Should i use another spritebatch to draw my background?..

EDIT: nevermind... will use lgScn2DImage

@Informatix
 
Last edited:
Upvote 0

abhishek007p

Active Member
Licensed User
Longtime User
@Informatix

We have a multi screen example but the screens are all in main activity, but what if we put those screens on a separate classes so it will be a bit more cleaner? is that possible? im trying to create a separate screen classes for;
  • Splash Screen
  • Home Menu (Game Menu)
  • Main Game
I am also using Scene2d Stage so i can use the built in actions and add simple effect (already done). but im not sure on how will i implement multi screen where each screens are on a separate classes. @Informatix , @andymc , @Erel , @AnyoneWhoKnowsLibGDX
 
Upvote 0

Informatix

Expert
Licensed User
Longtime User
@Informatix

We have a multi screen example but the screens are all in main activity, but what if we put those screens on a separate classes so it will be a bit more cleaner? is that possible?

Yes, and it's exactly what I did in my current game. The screen manager is declared in Main in Process Globals so it can be accessed from all classes to change the current screen.
 
Upvote 0

abhishek007p

Active Member
Licensed User
Longtime User
Yes, and it's exactly what I did in my current game. The screen manager is declared in Main in Process Globals so it can be accessed from all classes to change the current screen.

same with me, screen manager is on the main activity, but how will you call each screen? where should i put my screen selector (ScreenManager.CurrentScreen = ObjScreen)?

my screens: Splash, Home menu, game screen
 
Upvote 0

ivan.tellez

Active Member
Licensed User
Longtime User
I'm not sure of your question because the answer is contained in your question (ScreenManager.CurrentScreen = TheNewScreenToShow).

abhishek007p Is not asking right.

He has a game, with 3 different screens in one activity. He want to know, What is the correct way to split the code to avoid hundreds of lines of code in the main activity.

The main question he asked, is possible to put the code of each screen in a separate class?


Thanks
 
Upvote 0

Informatix

Expert
Licensed User
Longtime User
If the question is about events (Show/Hide/etc.), then the events are sent to Main because the screen manager is created in Main. In my game, I chose to redirect these events to the appropriate class so everything is in the class. It's easier to read, to maintain and the overhead is neglectable. In my code, in Main, I have many lines like this:
B4X:
Sub CAR_Show
    ClsMissionCarte.Show
End Sub

Sub CAR_Render(Delta As Float)
    ClsMissionCarte.Render
End Sub

Sub DECK_Show
    ClsPioche.Show
End Sub

Sub DECK_Render(Delta As Float)
    ClsPioche.Render
End Sub

Sub STAT_Show
    ClsStats.Show
End Sub

Sub STAT_Render(Delta As Float)
    ClsStats.Render(Delta)
End Sub

etc.
 
Upvote 0

abhishek007p

Active Member
Licensed User
Longtime User
I'm not sure of your question because the answer is contained in your question (ScreenManager.CurrentScreen = TheNewScreenToShow).

Sorry, but im asking on where should i put that line since i have 3 screens, what i did is put;

On Main Activity, Lgdx Create Event:
ScreenManager.CurrentScreen = Splash

On Main Activity, Lgdx Render Event:
After a few seconds, the splash screen will close (and get dispose) and should display the Home Screen/Game Menu Screen which I am not sure where to put it?
 
Upvote 0

Informatix

Expert
Licensed User
Longtime User
Sorry, but im asking on where should i put that line since i have 3 screens, what i did is put;

On Main Activity, Lgdx Create Event:
ScreenManager.CurrentScreen = Splash

On Main Activity, Lgdx Render Event:
After a few seconds, the splash screen will close (and get dispose) and should display the Home Screen/Game Menu Screen which I am not sure where to put it?
Sorry but I don't understand what you're asking. If you want to display a screen after another, then change the CurrentScreen value (a Hide event will be raised for the screen to hide and a Show event will be raised for the new screen). There's no particular place to change a screen. Change it when you need to change it. As your question is extremely vague, I cannot say more.
 
Upvote 0

abhishek007p

Active Member
Licensed User
Longtime User
Sorry but I don't understand what you're asking. If you want to display a screen after another, then change the CurrentScreen value (a Hide event will be raised for the screen to hide and a Show event will be raised for the new screen). There's no particular place to change a screen. Change it when you need to change it. As your question is extremely vague, I cannot say more.

so putting 'ScreenManager.CurrentScreen = GameMenuScreen' on the RENDER EVENT will not cause any problem?..
 
Upvote 0
Top