Android Question Libgdx: how to generate an event for Map_Exagonal

Carcas

Member
Licensed User
Longtime User
Hello everyone,

I created an hexagonal map as the example "Map_Hexagonal". (Libgdx example by Infomatrix)

I want to know how to add a "click" event to every single cell

or some other way to get the same result.

Thank You
 

Jeffrey Cameron

Well-Known Member
Licensed User
Longtime User
Look at Informatix's "Map tIDE" example in his example suite to see how to set up the lgGestureDector and apply that to your app. Then, in the GD_Touchdown event you can translate the touch to your grid co-ordinates using your Stage.ScreenToStageCoordinates method. Something like:
B4X:
Sub GD_TouchDown(X AsFloat, Y AsFloat, Pointer AsInt) As Boolean
     Dim poVect As lgMathVector2

     ' calculate where we touched
     poVect.X = X
     poVect.Y = Y
     poVect = MainStage.ScreenToStageCoordinates(poVect)
     Dim piX As Int = FromPixelToTile_X(poVect.X)
     Dim piY As Int = FromPixelToTile_Y(poVect.Y)
     LogColor("GD TOUCH Map Pos: " & piX & ", " & piY, Colors.Magenta)

     Return False
End Sub

If you are unfamiliar with the "FromPixel..." methods please see Informatix's PacDroid example.
 
Upvote 0

Carcas

Member
Licensed User
Longtime User
B4X:
Sub GD_TouchDown(X AsFloat, Y AsFloat, Pointer AsInt) As BooleanDim poVect AslgMathVector2
' calculate where we touched poVect.X = X
poVect.Y = Y
poVect = MainStage.ScreenToStageCoordinates(poVect)Dim piX As Int = FromPixelToTile_X(poVect.X)Dim piY As Int = FromPixelToTile_Y(poVect.Y)
LogColor("GD TOUCH Map Pos: " & piX & ", " & piY, Colors.Magenta)
ReturnFalseEnd Sub


does not exist in "Map_tIDE" example

?!?

ps: you must have an stage ??
 
Upvote 0

Jeffrey Cameron

Well-Known Member
Licensed User
Longtime User
Correct, but it does have a gesture detector and it shows you how to initialize it. You can add additional events for the detector to handle touches (this example) or panning, zooming, etc.

I'm not sure if a stage is required, I just use one to make things easier as it has its own viewport, spritebatch and camera objects.
 
Last edited:
Upvote 0

Carcas

Member
Licensed User
Longtime User
What is 'MainStage' in your project?

B4X:
Dim piX As Int = FromPixelToTile_X(poVect.X)
Dim piY As Int = FromPixelToTile_Y(poVect.Y)

FromPixelToTile give error. The compiler does not recognize it

Jefrey could you post the complete and running code?
I can not understand :(
 
Upvote 0

Jeffrey Cameron

Well-Known Member
Licensed User
Longtime User
MainStage is just a lgScn2DStage object. As for the "FromPixel..." methods, I already told you to take a look at Informatix's PacDroid example (click the word "PacDorid" in post #2). His PacDroid tutorial has a perfect example of those methods and he explains them very well too, if I recall correctly.

Giving any type of "complete and running code" for LibGDX is usually very problematic due to how the interface works. You really are much better off going through the examples Informatix has created and then combining the parts you need from his various example programs. To understand the examples, take the time to read through his LibGDX Tutorial it really does a good job of explaining the fundamentals.

Now, knowing which items you need to use when, that I am afraid I am still discovering myself ;)
 
Upvote 0
Top