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:

ivavilagu

Member
Licensed User
Longtime User
Hi everyone,

I have two questions

1-I've updated to 1.10 version and I´m not able to draw a lgParticleEffect with the new version. When I try to draw the effect in render event I receive an error. It worked with old version.

B4X:
SnowEffect.Draw2(Batch, DeltaTime) ' java.lang.IllegalArgumentException: sprite cannot be null.

2-I want to animate a character. I have some animations in a sprite sheet. Every animation has different width, so I need to line up the character using a center x variable ( frame_width / 2). Every time I change the animation, I call a function to calculate the new center of the frame and change the x position of the character.

B4X:
Sub LG_Render
'Some extra lines
    .
    .
    .
If (Hero.CurrentAnimation.IsAnimationFinished(Hero.StateTime) = True) Then                                                                           
                ChangeHeroCenter                'Change the x position of the character
                ChangeHeroAnimation(0)       'Change the animation of the character
End If

Batch.DrawRegion2(Hero.frame, Hero.xPOS, Hero.yPOS,Hero.frame.Regionwidth,Hero.frame.RegionHeight)

end sub

The problem is with the last frame before I change the animation.

Last frame before calling ChangeHeroCenter

The same frame after calling ChangeHeroCenter


Why can I see the last frame with the new position?. How can it be possible if batch drawing line has not been reached yet? It causes a visual glitch.
 
Last edited:

Informatix

Expert
Licensed User
Longtime User
1) The error is clearly stated: the sprite cannot be null.
I did not change anything to lgParticleEffect or lgSprite since long.

2) The code that changes the animation and the code that increases the state time are missing so I cannot answer.
 

Rantor777

Member
Licensed User
Hi to All

I am new to this Engine, @Informatix, How good is the Integration with Overlap2D ?

Can I use Overlap2D with your LibGDX Port using B4A ? If YES, Can you Provide an Example, Please .

Regards!
 

Informatix

Expert
Licensed User
Longtime User
Hi to All

I am new to this Engine, @Informatix, How good is the Integration with Overlap2D ?

Can I use Overlap2D with your LibGDX Port using B4A ? If YES, Can you Provide an Example, Please .

Regards!
Overlap2D is an editor and a runtime library. No one wrapped this library for B4A so Overlap2D is not directly usable, but as the exported files use the JSON format, it should not be complicated to use their contents in your game.
 

ivavilagu

Member
Licensed User
Longtime User
Sorry Informatix,

B4X:
Sub LG_Render
'Some extra lines
    .
    .
    .
     If (Hero.CurrentAnimation.IsAnimationFinished(Hero.StateTime) = True) Then                                                                           
                ChangeHeroCenter                'Change the x position of the character
                ChangeHeroAnimation(0)       'Change the animation of the character to idle animation
     End If
     Batch.DrawRegion2(Hero.frame, Hero.xPOS, Hero.yPOS,Hero.frame.Regionwidth,Hero.frame.RegionHeight)
End sub

Sub ChangeHeroAnimation(anim As Int)
    Hero.StateTime = 0                                                                               
    Hero.CurrentAnimation = Hero.Animation(anim)  'Where CurrentAnimation is a lgAnimation and animation() is a lgAnimation array
    If (anim = 0) Then
        Hero.Idle = True                    'Boolean that indicates if hero is in idle position
    Else
        Hero.Idle = False
    End If
End Sub

Sub ChangeHeroCenter
    If (Hero.idle = False) Then    'Hero.CenterX is a constant value from the idle animation ,created when animations are initialized. It means the idle animation frame width / 2
        If (Hero.CenterX < Hero.CurrentAnimation.GetKeyFrame(Hero.StateTime).RegionWidth / 2) Then
              Hero.xPOS = Hero.xPOS - Hero.centerx - (Hero.CurrentAnimation.GetKeyFrame(Hero.StateTime).RegionWidth / 2)
        Else
            Hero.xPOS = Hero.xPOS + Hero.centerx - (Hero.CurrentAnimation.GetKeyFrame(Hero.StateTime).RegionWidth / 2)
        EndIf
    Else
        If (Hero.CenterX < Hero.CurrentAnimation.GetKeyFrame(Hero.StateTime).RegionWidth / 2) Then
            Hero.xPOS = Hero.xPOS + Hero.centerx + (Hero.CurrentAnimation.GetKeyFrame(Hero.StateTime).RegionWidth / 2)
        Else
            Hero.xPOS = Hero.xPOS - Hero.centerx + (Hero.CurrentAnimation.GetKeyFrame(Hero.StateTime).RegionWidth / 2)
        EndIf
    EndIf
EndSub

As I said before, I don´t know why the last frame of an animation is shown after calling ChangeHeroCenter if batch.drawregion2 has not been called yet.
 

Informatix

Expert
Licensed User
Longtime User
I don't see any code setting Hero.Frame, which is the frame drawn by DrawRegion2.
 

ivavilagu

Member
Licensed User
Longtime User
I don't see any code setting Hero.Frame, which is the frame drawn by DrawRegion2.

B4X:
Sub LG_Render
    If (Hero.Idle = False) Then
         Hero.Frame = Hero.CurrentAnimation.GetKeyFrame(Hero.StateTime)
         If (Hero.CurrentAnimation.IsAnimationFinished(Hero.StateTime) = True) Then                                                                         
              ChangeHeroCenter                'Change the x position of the character
              ChangeHeroAnimation(0)       'Change the animation of the character to idle animation
         End If
     else
           Hero.frame = Hero.CurrentAnimation.GetKeyFrame2(Hero.StateTime, True) 'Idle animation is in loop mode
     end if
     Batch.DrawRegion2(Hero.frame, Hero.xPOS, Hero.yPOS,Hero.frame.Regionwidth,Hero.frame.RegionHeight)
End sub
 

Informatix

Expert
Licensed User
Longtime User
The explanation is in the code above. When you change the animation, you don't update Hero.Frame.
 

ivavilagu

Member
Licensed User
Longtime User
The explanation is in the code above. When you change the animation, you don't update Hero.Frame.

Ups!!!! What a mistake!

Thanks Informatix, it´s work fine now

I´m still fighting with lgParticle. I don´t know why does´t work with the new library version

Initializing the effect at start of app
B4X:
SnowEffect.Initialize                                                            
SnowEffect.load(lGdx.Files.internal("effects/snow"), lGdx.Files.internal("effects"))
SnowEffect.SetPosition(poly.Polyline.X * UnitScale, UpLimit * UnitScale)
SnowEffect.Start

The next line is between batch.begin and batch.end. I don´t know why batch is null. At the moment I only use batch in one line to draw hero.frame as you see in last post

B4X:
SnowEffect.Draw2(Batch, lGdx.Graphics.DeltaTime)

If I change the line the error disappear (obviously the effect cannot display)

B4X:
SnowEffect.Draw(Batch)

I try to log Batch content
B4X:
If (Batch <> Null ) Then
      Log("yes")
 End If
SnowEffect.Draw2(Batch,lGdx.Graphics.DeltaTime)

Surprisingly Batch is not null according log window
 

Informatix

Expert
Licensed User
Longtime User
It's not Batch that is null, it's the sprite drawn. Are you sure that all images are loaded and ready to use?
You should try your effect and images with the Particles demo of the library.
 

ivavilagu

Member
Licensed User
Longtime User
It's not Batch that is null, it's the sprite drawn. Are you sure that all images are loaded and ready to use?
You should try your effect and images with the Particles demo of the library.
The effect worked well until upgrade the library, I had been using 1.04 version. This is the reason I though the problem was the update. I will check path and files but it's all very strange. Maybe it's a deltatime problem.
 

Rantor777

Member
Licensed User
How to use Skins with LibGDX (Without INLINE JAVA) ?

PHP:
private void createBasicSkin(){
  //Create a font
  BitmapFont font = new BitmapFont();
  skin = new Skin();
  skin.add("default", font);
  //Create a texture
  Pixmap pixmap = new Pixmap((int)Gdx.graphics.getWidth()/4,(int)Gdx.graphics.getHeight()/10, Pixmap.Format.RGB888);
  pixmap.setColor(Color.WHITE);
  pixmap.fill();
  skin.add("background",new Texture(pixmap));
  //Create a button style
  TextButton.TextButtonStyle textButtonStyle = new TextButton.TextButtonStyle();
  textButtonStyle.up = skin.newDrawable("background", Color.GRAY);
  textButtonStyle.down = skin.newDrawable("background", Color.DARK_GRAY);
  textButtonStyle.checked = skin.newDrawable("background", Color.DARK_GRAY);
  textButtonStyle.over = skin.newDrawable("background", Color.LIGHT_GRAY);
  textButtonStyle.font = skin.getFont("default");
  skin.add("default", textButtonStyle);
}
 
Last edited:

Informatix

Expert
Licensed User
Longtime User
You cannot use skins. I ignored this feature. Never understood the real interest of it.
 

ilan

Expert
Licensed User
Longtime User
hi

i have a probelm with a revolutejoint

i made a circle and a box and i connect them with a revolutejoint. i set the revolutejointdef and create it with World.CreateJoint(RevoluteJointDef)
everything is perfect until now.
i also set the RevoluteJointDef properties (motorspeed, maxMotorTorque,...) when i create my 2 bodies and the joint i set the enableMotor to false

now i would like to turn it on when i press on a button so i have to create a reference to the created joint and i do it like this

in globals i write:

B4X:
 Dim vehicle_motor As lgBox2DRevoluteJoint

now when i create the bodies and the joint i write:

B4X:
vehicle_motor = World.CreateJoint(RevoluteJointDef)

but this gives me an error. turning on the motor when i create the joint works but i want to control it after creating the joint.


how can i do it??

thank you

Solved it
 
Last edited:

wonder

Expert
Licensed User
Longtime User
A few quick questions:

1. How to exit a LibGDX based app? Activity.Finish or ExitApplication?
2. Should I pause LibGDX before exiting?
3. Should I manually call LG_Dispose before exiting?

Thanks in advance!
 

Informatix

Expert
Licensed User
Longtime User
A few quick questions:

1. How to exit a LibGDX based app? Activity.Finish or ExitApplication?
2. Should I pause LibGDX before exiting?
3. Should I manually call LG_Dispose before exiting?

Thanks in advance!
1) Activity.Finish. It's not different from other apps.
2) No. Why?
3) No. It is automatically called. Look at the life-cycle in the libGDX introduction guide.
 

ilan

Expert
Licensed User
Longtime User
in my opinion activity.finish should be called when user want to exit the game and not on Home Button click or incoming call...

i put a menu inside the game and when the user want to exit he need to confirm that.

so i dont put Activity.finish in Activity_Pause
 
Cookies are required to use this site. You must accept them to continue using the site. Learn more…