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:

TheJinJ

Active Member
Licensed User
Longtime User
Managed to get this to work for a body, I made a silly mistake by passing the userdata after createfixture as per most examples on the web. Doc says that body.userdata is for getting the data.

B4X:
For i = 0 To 3
    bodyDef.position.Set(Rnd(-10, 10), Rnd(-10, 10))
    bodya(i) = world.CreateBody(bodyDef)
    bodya(i).UserData = image0
    bodya(i).createFixture(fixtureDef) '.UserData = image0
    bvec2 = bodya(i).Position
    image0.SetBounds(bvec2.x - (aspect_ratio * scale) / 2, bvec2.y - scale / 2,  aspect_ratio * scale , scale) ' x y w h
Next

Then in spritebatch....

B4X:
    sb.Begin
        For i = 0 To 3
            bvec2 = bodya(i).Position
            tmpimg = bodya(i).UserData
            tmpimg.setPosition(bvec2.x - tmpimg.Width /2, bvec2.y - tmpimg.Height /2)
            tmpimg.SetOriginCenter
            tmpimg.Rotation = bodya(i).Angle * lgmu.radiansToDegrees
            tmpimg.Draw(sb)
        Next
    sb.End
 

Informatix

Expert
Licensed User
Longtime User
Yes, I saw there's a little mistake in the doc. It should say: "get or set..."
 

Informatix

Expert
Licensed User
Longtime User
I'm not at a PC but a thought, does user data after createfixture set userdata on the fixture, not the body?
CreateFixture initializes a new fixture so, after calling it, the UserData property of the fixture is Null. But that concerns only the property of this class, not the UserData property of your body. There's no relation between these two properties. UserData is exactly like Tag for your other views. Nothing sets its value except you.
 

TheJinJ

Active Member
Licensed User
Longtime User
It seems that body.userdata is for the body, passing after createfixture sets it for the fixture, tested with contact listeners retrieving the body userdata and the fixture separately.

B4X:
For i = 0 To 1
    bodyDef.position.Set(Rnd(-10, 10), Rnd(-10, 100))
    bodya(i) = world.CreateBody(bodyDef)
    bd_data.sprite = image0
    bd_data.bname = "Sprite_Can" & i
    bodya(i).UserData = bd_data
    bodya(i).createFixture(fixtureDef).UserData = "Object"
    bvec2 = bodya(i).Position
    image0.SetBounds(bvec2.x - (aspect_ratio * scale) / 2, bvec2.y - scale / 2,  aspect_ratio * scale , scale) ' x y w h
Next

B4X:
    fa = Contact.FixtureA
    fb = Contact.FixtureB
      
    If fa.UserData <> Null Then
   
        Select fa.UserData
            Case "Ground"
                Log("FA: " & fa.UserData)
            Case "Object"
                Log("FA: " & fa.UserData)
 
Last edited:

Informatix

Expert
Licensed User
Longtime User
It seems that body.userdata is for the body, passing after createfixture sets it for the fixture, tested with contact listeners retrieving the body userdata and the fixture seperately.
???
When you set Body.UserData, it's for the body and nothing else, whatever may be the place where you set it. CreateFixture does not change this property and has no reason to do so.
 

TheJinJ

Active Member
Licensed User
Longtime User
EDIT
So the createfixture.userdata is for the fixture....got it now. I was confused initially where I thought only the body could hold userdata and that using createfixture.userdata was just chained onto body thus setting it's property....
Thanks for your help
 
Last edited:

RichyK68

Active Member
Licensed User
Longtime User

Hi, I'm trying to load a tmx file saved from Tiled Map Editor, it is called map_1.tmx and it's located in the assets folder of my app.

B4X:
    Dim loader As lgMapTmxMapLoader
    CurrentMap = loader.Initialize2("map_1.tmx")

The file is definitely there, but I get the same errors (below) whether I feed it the correct name of the file or a file that doesn't even exist, which leads me to believe it can't even see it.

Does anybody have any ideas why?

Error occurred on line: 725 (main)
com.badlogic.gdx.utils.SerializationException: Error parsing file: map_1.tmx
at com.badlogic.gdx.utils.XmlReader.parse(SourceFile:83)
at com.badlogic.gdx.maps.tiled.TmxMapLoader.load(SourceFile:122)
at com.badlogic.gdx.maps.tiled.TmxMapLoader.load(SourceFile:110)
at anywheresoftware.b4a.libgdx.maps.lgTmxMapLoader.Load(SourceFile:32)
at anywheresoftware.b4a.libgdx.maps.lgTmxMapLoader.Initialize2(SourceFile:21)
at richkidsoftware.platform.main._lg_create(main.java:2550)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at anywheresoftware.b4a.shell.Shell.runMethod(Shell.java:636)
at anywheresoftware.b4a.shell.Shell.raiseEventImpl(Shell.java:302)
at anywheresoftware.b4a.shell.Shell.raiseEvent(Shell.java:238)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at anywheresoftware.b4a.ShellBA.raiseEvent2(ShellBA.java:121)
at anywheresoftware.b4a.ShellBA$1.run(ShellBA.java:66)
at android.os.Handler.handleCallback(Handler.java:733)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:136)
at android.app.ActivityThread.main(ActivityThread.java:5105)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:792)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:608)
at dalvik.system.NativeStart.main(Native Method)
Caused by: com.badlogic.gdx.utils.GdxRuntimeException: Error reading file: map_1.tmx (Internal)
at com.badlogic.gdx.backends.android.AndroidFileHandle.read(SourceFile:76)
at com.badlogic.gdx.files.FileHandle.reader(SourceFile:187)
at com.badlogic.gdx.utils.XmlReader.parse(SourceFile:81)
... 23 more
Caused by: java.io.FileNotFoundException: map_1.tmx
at android.content.res.AssetManager.openAsset(Native Method)
at android.content.res.AssetManager.open(AssetManager.java:323)
at android.content.res.AssetManager.open(AssetManager.java:297)
at com.badlogic.gdx.backends.android.AndroidFileHandle.read(SourceFile:74)
... 25 more
** Activity (main) Resume **
 

Informatix

Expert
Licensed User
Longtime User
Could you post the TMX file?
 

TheJinJ

Active Member
Licensed User
Longtime User
FileNotFoundException: map_1.tmx

I think you need to specify the internal directory where the file is located

 

RichyK68

Active Member
Licensed User
Longtime User
Thanks for the quick replies. I've also tried

B4X:
CurrentMap = loader.Initialize2(lGdx.Files.internal("map_1.tmx"))

which is the technique used for texture loading, but this didn't work either. I'll post the tmx file, there's also a tsx and a png.
 

RichyK68

Active Member
Licensed User
Longtime User
Here are the three files that exist in my assets folder (located in the files folder of my B4A project).
 

Attachments

  • files.zip
    126.3 KB · Views: 161

RichyK68

Active Member
Licensed User
Longtime User
Yeah that was my second attempt, the first one fails with the errors (below). My first attempt doesn't use the tsx only a png.

Timeline: Activity_launch_request id:richkidsoftware.platform time:180088926
** Activity (main) Pause, UserClosed = false **
showStatusIcon on inactive InputConnection
** Activity (main) Create, isFirst = true **
java.lang.NullPointerException
java.lang.NullPointerException
** Activity (main) Resume **
sensor listener setup
OGL renderer: Adreno (TM) 330
OGL vendor: Qualcomm
OGL version: OpenGL ES 3.0 V@66.0 AU@ (CL@)
OGL extensions: GL_AMD_compressed_ATC_texture GL_AMD_performance_monitor GL_AMD_program_binary_Z400 GL_EXT_debug_label GL_EXT_debug_marker GL_EXT_discard_framebuffer GL_EXT_robustness GL_EXT_texture_format_BGRA8888 GL_EXT_texture_type_2_10_10_10_REV GL_NV_fence GL_OES_compressed_ETC1_RGB8_texture GL_OES_depth_texture GL_OES_depth24 GL_OES_EGL_image GL_OES_EGL_image_external GL_OES_element_index_uint GL_OES_fbo_render_mipmap GL_OES_fragment_precision_high GL_OES_get_program_binary GL_OES_packed_depth_stencil GL_OES_depth_texture_cube_map GL_OES_rgb8_rgba8 GL_OES_standard_derivatives GL_OES_texture_3D GL_OES_texture_float GL_OES_texture_half_float GL_OES_texture_half_float_linear GL_OES_texture_npot GL_OES_vertex_half_float GL_OES_vertex_type_10_10_10_2 GL_OES_vertex_array_object GL_QCOM_alpha_test GL_QCOM_binning_control GL_QCOM_driver_control GL_QCOM_perfmon_global_mode GL_QCOM_extended_get GL_QCOM_extended_get2 GL_QCOM_tiled_rendering GL_QCOM_writeonly_rendering GL_EXT_sRGB GL_EXT_sRGB_write_control GL_EXT_texture_sRGB_decode GL_EXT_texture_filter_anisotropic GL_EXT_multisampled_render_to_texture GL_EXT_color_buffer_float GL_EXT_color_buffer_half_float GL_EXT_disjoint_timer_query
framebuffer: (5, 6, 5, 0)
depthbuffer: (16)
stencilbuffer: (0)
samples: (0)
coverage sampling: (false)
Managed meshes/app: { }
Managed textures/app: { }
Managed shaders/app: { }
Managed buffers/app: { }
Error occurred on line: 725 (main)
com.badlogic.gdx.utils.SerializationException: Error parsing file: map_1.tmx
at com.badlogic.gdx.utils.XmlReader.parse(SourceFile:83)
at com.badlogic.gdx.maps.tiled.TmxMapLoader.load(SourceFile:122)
at com.badlogic.gdx.maps.tiled.TmxMapLoader.load(SourceFile:110)
at anywheresoftware.b4a.libgdx.maps.lgTmxMapLoader.Load(SourceFile:32)
at anywheresoftware.b4a.libgdx.maps.lgTmxMapLoader.Initialize2(SourceFile:21)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at anywheresoftware.b4a.shell.Shell.runMethod(Shell.java:636)
at anywheresoftware.b4a.shell.Shell.raiseEventImpl(Shell.java:302)
at anywheresoftware.b4a.shell.Shell.raiseEvent(Shell.java:238)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at anywheresoftware.b4a.ShellBA.raiseEvent2(ShellBA.java:121)
at anywheresoftware.b4a.ShellBA$1.run(ShellBA.java:66)
at android.os.Handler.handleCallback(Handler.java:733)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:136)
at android.app.ActivityThread.main(ActivityThread.java:5105)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:792)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:608)
at dalvik.system.NativeStart.main(Native Method)
Caused by: com.badlogic.gdx.utils.GdxRuntimeException: Error reading file: map_1.tmx (Internal)
at com.badlogic.gdx.backends.android.AndroidFileHandle.read(SourceFile:76)
at com.badlogic.gdx.files.FileHandle.reader(SourceFile:187)
at com.badlogic.gdx.utils.XmlReader.parse(SourceFile:81)
... 22 more
Caused by: java.io.FileNotFoundException: map_1.tmx
at android.content.res.AssetManager.openAsset(Native Method)
at android.content.res.AssetManager.open(AssetManager.java:323)
at android.content.res.AssetManager.open(AssetManager.java:297)
at com.badlogic.gdx.backends.android.AndroidFileHandle.read(SourceFile:74)
... 24 more
** Activity (main) Resume **
Timeline: Activity_idle id: android.os.BinderProxy@428c2340 time:180092460
 
Cookies are required to use this site. You must accept them to continue using the site. Learn more…