Android Tutorial Introduction to the libGDX library

ilan

Expert
Licensed User
Longtime User
is there any event for lgInputProcessor or lgGestureDetector that recognize if my finger is touching a specific area.
i dont mean thouchdown, because only when i touch it, it will be fired i mean when i still hold my finger on the same point

i tried to set a boolean to true when touchdown and then to false when touchup but the problem is, i have 4 buttons and i touch 2 at the same time
and on touch up all booleans goes to false and this is not what i want.

and if i would check the touchup (if it was on a specific area) this will also not be a good choice because i have in my game a gas pedal and a break pedal (+2 other buttons) if the user touch the break and then slide his finger out of the button area and then touch up the gas boolean will still be true and that is wrong
so i need to set all booleans to false on touchup but then if i cannot press 2 buttons simultaneously

what should i do? use the regular activity touch event?
 

Informatix

Expert
Licensed User
Longtime User
You should manage the touch events at the actor level, not at the screen level. Each Scene2D actor has its own event, so no confusion, no pointer handling, nothing special to do...

NEVER USE the activity events in a game made with libGDX, especially Keypress. It's a good way to get mysterious crashes.
 

ilan

Expert
Licensed User
Longtime User
i dont know if i understood you correctly
but what you mean is i need to create for each button a different Scene2d (lgScn2DActor) and handle the touch on it?
 

Informatix

Expert
Licensed User
Longtime User
i dont know if i understood you correctly
but what you mean is i need to create for each button a different Scene2d (lgScn2DActor) and handle the touch on it?
A lgScn2DButton is more appropriate for a button, but yes, that's the idea.
The HUD and all interactions with the player should be handled with Scene2D. Personally, I use Scn2D for almost everything. It makes things so simple. I just avoid it for parts that really needs performance.
 

ilan

Expert
Licensed User
Longtime User
thank you very much @Informatix, i did not knew libgdx got his own controls like button.
can you tell me please how the event of the lgbutton look like?

or is it just Sub Lgbtn_CLick ?

EDIT: or does your libgdx example got a button in it? if yes could you point me to that example? thank you
 
Last edited:

ilan

Expert
Licensed User
Longtime User
Try the various demos.

ok, i looked on some projects and i could found how to add a button and handle the event i also looked at the touchpad example and window example.
i could not figure out if the button has a touchdown/up event and how that event look like

so what i did i add a lgScn2DInputListener and handle the touchdown/up from there but is this the right way?
or does the button has his own touchdown/up events?

is there anywhere a documentation about all events for all libgdx controls?

thank you,
 

Informatix

Expert
Licensed User
Longtime User
This is covered in details in this thread. Just read post #3.
 

melonZgz

Active Member
Licensed User
Longtime User
I'm not sure I understand the dispose event.
In the dispose event I dispose everything I can. I suppose it's correct (spritebatch, box2D world... everything witch has a dispose method)
But, when is it called? Because sometimes it's called after the pause event. Not usually, but sometimes it does. And it's a problem for me, because if it's called, in the resume event I have to create everything again. If I'm in a menu it doesn't care much, but if I am ingame (accidental home button, income call, or even when showing an ad), in some of those cases, after pause, if dispose is called, my Box2d world is disposed, and I have to create everything again? or restart the level?
Is it a problem if I don't dispose my box2d world?
 

ilan

Expert
Licensed User
Longtime User
income call, or even when showing an ad)

if Not(UserClosed) then ...
you can check if the user closed that activity and not an incoming call or an ad...

you can also catch the Back key (sub Activity_KeyPress) and ask if user want to exit the game set a boolean to true and if the user exits in a different way like home key that boolean will be false and u continue from the same point you left (don't dispose in that case).
 

Informatix

Expert
Licensed User
Longtime User
It's clearly explained in the guide:
Dispose :

Dispose is called when the activity is exited, after Pause, or when the device is rotated.
In this event, release all the used resources by calling the Dispose function of objects (if they have one).
"Dispose" is required to deallocate the memory because libGDX calls internally a lot of C code and this C code does not benefit from a garbage collector. This offers a great advantage (fast code, all memory available) but has this minor drawback (memory has to be released explicitely when no longer used).

As suggested by Ilan, you can avoid calling the dispose routines by checking UserClosed.
 

Informatix

Expert
Licensed User
Longtime User
It is dangerous to handle the Back Key with the Activity_KeyPress event in a libGdx game because all events are supposed to be seen and processed by the libGDX engine. You can have unexplained crashes when leaving a screen or an app by doing that as libGDX was not able to perform some internal operations.
The safe way is to handle this key in the Render event with lGdx.Input.IsKeyJustPressed(KeyCodes.KEYCODE_BACK). You have to set lGdx.Input.CatchBackKey to True before. You can also handle this key with the KeyPressed event of lgInputProcessor.
 

ilan

Expert
Licensed User
Longtime User

thanx @Informatix, until today i used the backkey to exit my libgx game activity and i had never a crash report but i will definitely use your suggestion for my next libgdx box2d game
 
Last edited:

melonZgz

Active Member
Licensed User
Longtime User
About rendering tiled maps:
It is also possible to create a texture atlas from an existing map with the TiledMapPacker tool. I recommend using it when you have visual issues while zooming or rescaling the map as it duplicates the edges of your tiles.

Only wanted to say that I've been facing this problem (visual issues, you can see the edges of some tiles) and the problem isn't there if you use power of 2 textures. I don't know if it's been said before, but It was driving me nuts until I saw the solution.
 

wonder

Expert
Licensed User
Longtime User

I had the same problem, keyword: had.

a) What kind of value goes into your DrawRegion() or DrawTex() commands?
Is it a (result of) Int, Float or Double (calculations)?

b) Are you expriencing this problem with or without the Linear filter?

c) Are your texutures padded at least 2px away from the edges and each-other?
 

wonder

Expert
Licensed User
Longtime User
This is almost mandatory when you deal with tiles. Without this padding, you're pretty sure to have visual issues on some devices.
Yep this and also making sure that, if you're using floats or doubles for your tile placement, the drawing "step" must be constant.

In my case:
BAD (before):
- Calculate tile placement (x, y) As Double
- Draw at (x, y)

GOOD (after):
- Calculate the placement of the first tile (x, y)
- Draw all the remainng tiles at (double) FirstTile_Offset(x, y) + (int) TileIterator(x, y) * (int) TileSize(x, y)

Example:
B4X:
FirstTileX = 23.56
FirstTileY = 87.489

Dim w, h as Int
Dim x, y as Double
For ix = 0 To (maxHorizontalTilesOnScreen - 1)
    For iy = 0 To (maxVerticalTilesOnScreen - 1)
        w = TileWidth
        h = TileHeight
        x = FirstTileX + (ix * w)
        y = FirstTileY + (iy * h)
        Batch.DrawRegion2(tile, x, y, w, h)
    Next
Next
 
Cookies are required to use this site. You must accept them to continue using the site. Learn more…