B4A Library libGDX - Game Engine



One of the best game engines for Android is now available to B4A users. Unleash your creativity!

You can read a description of this library in this guide.

Download for B4A v10.60 and older (4 MB)
Download for B4A v10.70 and newer (4 MB)

Starting from version 1.13, the library is compiled with Java 8, and thus requires a recent version of B4A and Java 8+.
To install the library, copy the .jar file and the .xml file in your libraries folder.

This library is not compatible with the Debug mode. Ensure that you always compile in Release mode.

Download also the examples and templates.

You can reduce the weight of the library if you want to keep your APK file as small as possible. Read this post to learn how to create your Lite version.

Tutorials:
How to make games
Introduction to the libGDX library
PacDroid: from scratch to fun
Shaders

Take also a look at the Cloney Bird tutorial by andymc.

Plugins:
Box2DLights
SpecialFX

Versions:
 
Last edited by a moderator:

LucaMs

Expert
Licensed User
Longtime User
I state that I never developed games but sometimes I took a look at these tools.

Although I am inexperienced, this seems like a great job.

I have a couple of questions optional, regarding your app "My Playground (Free)" (I hope that Erel not ask me to move them); it is really very pretty.

When you published the app? I can read but not update the date of the publication date.

The graphics: have you drawn everything? Use tools such as graphics tablets?

Although I will not get answers, congratulations for your great job!
 

sterlingy

Active Member
Licensed User
Longtime User
In playing with the Pacdroid code, I tried to replace the maze map with a map of my own, but since you have properties embedded in the map and tileset, things aren't working out so well. However, just to see what would happen, I loaded your map assets into Tiled, doubled the height of the map, and filed in the blank area with tiles. This resulted in the map being scaled down so that it could fit the entire map in the viewport.

In other tests, I have been able to scale the tiled map, but I wasn't using Scn2D and Stage. If possible, how can I fill the width of the viewport with the width of the map, so I can move the viewport over the map to reveal different parts of the map?



I wasn't sure where this post should go. In this thread or the Pacdroid Tutorial thread. Seems like it could go in either.
 

sterlingy

Active Member
Licensed User
Longtime User
Okay, feel better.
 

sterlingy

Active Member
Licensed User
Longtime User

Informatix,

I did ask on the libGDX forum, but no one had a clue, though one guy had experienced the same issue.

I did resolve my problem, however. My TileSet had each tile paced by one pixel. The gaps between the tiles were filled with black. With the Tiled Editor compensating for this, everything looked fine in the editor. I made a new TileSet, without gaps, and the problem has gone away. The "rounding" issue may still be present, but no one would be the wiser.

-Sterlingy
 

sterlingy

Active Member
Licensed User
Longtime User
I'm making progress with this lib, but my code seems to want to crash periodically. Sometimes, it's when I exit. Sometimes when I try to run it after the install. In this case, it will almost always run fine when I launch it from my device's panel.

I get an IndexOutOfBoundsException error

B4X:
Sub Main_Draw(SpriteBatch As lgSpriteBatch, ParentAlpha As Float)
    'Draws the maze
    SpriteBatch.DisableBlending
    SpriteBatch.SetColorRGBA(1, 1, 1, 1)
    myMap.Draw(Stage.Camera)
    SpriteBatch.EnableBlending
End Sub

The error occurs on the EnableBlending line.

What could cause this, as it doesn't happen all of the time?

I should point out that I compile and run in debug mode, and I think I read somewhere that the debugger has issues with libGDX.

Cheers,

Sterling
 

sterlingy

Active Member
Licensed User
Longtime User
I hope you've recovered. I've been battling something all day with two stages, and actors/sprites not rendering on the back stage.

I have two stages. One has a tiled map and a bunch of sprites moving around. On top of that, I have a HUD stage, which has the score and other things.

I can see the tiled map, but the sprites aren't visible. If I don't draw the HUD stage, then I can see the sprites. No one on the libGDX board has a clue

The HUD is just some text in a table, so it is largely transparent as you will see below (guess whose tutorial I used as a base? )

This code:
B4X:
Sub Main_Draw(SpriteBatch As lgSpriteBatch, ParentAlpha As Float)
    'Draws the maze
    SpriteBatch.DisableBlending
    SpriteBatch.SetColorRGBA(1, 1, 1, 1)
    Maze.Draw(Stage.Camera)
    SpriteBatch.EnableBlending
    HUDStage.Draw
End Sub
Produced this image, when you can see the Map on the background stage. You can also see the Score and three remaining lives in the HUDStage



This code, where I do not draw the HUD stage, reveals the sprites, which are in a group and actors of the Maze Stage, along with the Map
B4X:
Sub Main_Draw(SpriteBatch As lgSpriteBatch, ParentAlpha As Float)
    'Draws the maze
    SpriteBatch.DisableBlending
    SpriteBatch.SetColorRGBA(1, 1, 1, 1)
    Maze.Draw(Stage.Camera)
    SpriteBatch.EnableBlending

End Sub



This code , where blending is on for everything, results in the HUD and nothing else

B4X:
Sub Main_Draw(SpriteBatch As lgSpriteBatch, ParentAlpha As Float)
    'Draws the maze
    SpriteBatch.EnableBlending
    SpriteBatch.SetColorRGBA(1, 1, 1, 1)
    Maze.Draw(Stage.Camera)
    HUDStage.Draw
End Sub
 

Informatix

Expert
Licensed User
Longtime User

I'm not sure to understand your question. If you want to move the camera over the map to reveal different parts, then the map has to be larger than the viewport. Example with PacDroid: in Tiled, I resize the map width to 56 tiles (twice the former value) and I put new tiles in the empty space. In the clsMaze class, I divide MapWidth by 2. So now my app displays only the half of my map. If I translate the camera to the right, I can reveal the invisible part.
 

Informatix

Expert
Licensed User
Longtime User
There's something that annoys me in your code. Each stage has its own Spritebatch, but in your code, you use the same for the maze and the HUD. If you want to share the same batcher, use Stage.Initialize3 to set yourself the SpriteBatch.
 

sterlingy

Active Member
Licensed User
Longtime User
The tiledMap scrolls just fine, as I change the position of the camera:
B4X:
Public Sub Draw(Camera As lgOrthographicCamera)
Camera.Position.y = Camera.Position.y + ScrollSpeed
    MapRenderer.SetCameraView(Camera)
    MapRenderer.renderTileLayer(CurrentLayer)
End Sub

The tiledMap is in a group with the actors/sprites, and are on the bottom stage. the HUD is on the top stage. This has the score and the remaining lives. In the top picture, seen two posts up, you will see the content of the HUD stage, on top of the map, on the bottom stage, but the actors and sprites don't render.

If I don't draw the HUD stage, then it renders all of the children of the bottom stage, not just the tiled map.

Does that make more sense?

-SterlingY
 

sterlingy

Active Member
Licensed User
Longtime User
There's something that annoys me in your code. Each stage has its own Spritebatch, but in your code, you use the same for the maze and the HUD. If you want to share the same batcher, use Stage.Initialize3 to set yourself the SpriteBatch.

So you suggest that I create a different SpriteBatch for each Stage, as the best solution?

I looked at stage.initialize3. I'm not sure what EvtPrefix is? If my SpriteBatch is called "GloriousBatchSprite", and the EvtPrefix is "notSo",
then to render this stage would be notSoGloriousSpriteBatch.Draw?

-SterlingY
 

Informatix

Expert
Licensed User
Longtime User
So you suggest that I create a different SpriteBatch for each Stage, as the best solution?

No, the contrary: use the same Spritebatch, but you have to share it explicitely because a call to Initialize will create a new one.
The current situation is that you created two SpriteBatchs but you try to use the same in your Draw event.
What I suggest is to create only one with Initialize and to pass the created SpriteBatch to the other stage with Initialize3.

I looked at stage.initialize3. I'm not sure what EvtPrefix is? If my SpriteBatch is called "GloriousBatchSprite", and the EvtPrefix is "notSo",
then to render this stage would be notSoGloriousSpriteBatch.Draw?
The event is Draw, so if the event prefix is "toto", the event handler should be named "toto_Draw".
In PacDroid, Main_Draw is the draw event of grpMain (declared with grpMain.Initialize("Main")).
 

sterlingy

Active Member
Licensed User
Longtime User
So here's what I did

B4X:
    'Initializes the stage
    Stage.Initialize("")
    HUDStage.Initialize3(lGdx.Graphics.Width, lGdx.Graphics.Height, True, Stage.SpriteBatch, "HUD")
.
.
.
Sub Main_Draw(SpriteBatch As lgSpriteBatch, ParentAlpha As Float)
    'Draws the maze
    SpriteBatch.DisableBlending
    SpriteBatch.SetColorRGBA(1, 1, 1, 1)
    Maze.Draw(Stage.Camera)
    SpriteBatch.EnableBlending
'    HUDStage.Draw
End Sub

Sub HUD_Draw(SpriteBatch As lgSpriteBatch, ParentAlpha As Float)
    'Draws the HUD
    SpriteBatch.EnableBlending
    SpriteBatch.SetColorRGBA(1, 1, 1, 1)
    HUDStage.Draw
End Sub

All I get is the background stage with the map.

Sorry I'm such a pain. Just a few more hurdles, and I can focus on the visually creative stuff, which I know well.
 

Informatix

Expert
Licensed User
Longtime User
Do you call Draw for your HUD in the Render event?

And if you share the same SpriteBatch, what's the reason for two separate draw events?
 

sterlingy

Active Member
Licensed User
Longtime User
I did not Draw the HUD in the Render Event. I was under the assumption I had to do it the same way I do it for Stage. Adding HUDStage.Draw to the Render event, and removing the HUD_Draw sub, gives me a blank screen.
 
Cookies are required to use this site. You must accept them to continue using the site. Learn more…