Android Question [LibGdx] How to Handle Touch Event?

abhishek007p

Active Member
Licensed User
Longtime User
Hi! I am cloning a balloon Smasher game and here is the screenshot of my current work.

Countdown timer is also implemented using the class i created to handle it, you can find it here


5.png



This is the declaration for my balloon object:

B4X:
Type typBalloon(x As Float, y As Float, BalloonTexture As lgTexture)
Dim ListOfBalloon    As List

so all those balloons on the screen are created every 0.8 Seconds and added on the ListOfBalloon including the x, y, and texture data.

My problem now is how will i handle touch/tap event? like when user taps a balloon?
Is there any event or event handler for lgTexture object?

Im still starting out with libGdx so I hope you can help me.

thanks,

@Informatix
 

abhishek007p

Active Member
Licensed User
Longtime User
SOLVED.

The solution i came up with is use lgSprite object as my cursor, but i will also need to change my other object to lgSprite that i want to check for touch detection.

then on the render event, i could just use isTouched or Touched from LibGdx Object like this and set my cursor position

B4X:
If lGdx.Input.isTouched Then
         Dim v As lgMathVector2 = TranslateCoordinates(lGdx.Input.X, lGdx.Input.Y)
         sprCursor.SetPosition(v.X, v.Y)
End If

and then check if my cursor touched something..

B4X:
If sprCursor.BoundingRectangle.overlaps(sprMusic.BoundingRectangle) Then
'Cursor touched something......
End If

B4X:
Sub TranslateCoordinates(X As Int, Y As Int) As lgMathVector2
    Dim p As lgMathVector2
    p.X = vpWidth * (X / lGdx.Graphics.Width)
    p.Y = vpHeight - (vpHeight * (Y / lGdx.Graphics.Height)) 'flip Y coords
    Return p
End Sub

Please note that sprCursor and sprMusic are both lgSprite objects. :)
 
Upvote 0

walterf25

Expert
Licensed User
Longtime User
SOLVED.

The solution i came up with is use lgSprite object as my cursor, but i will also need to change my other object to lgSprite that i want to check for touch detection.

then on the render event, i could just use isTouched or Touched from LibGdx Object like this and set my cursor position

B4X:
If lGdx.Input.isTouched Then
         Dim v As lgMathVector2 = TranslateCoordinates(lGdx.Input.X, lGdx.Input.Y)
         sprCursor.SetPosition(v.X, v.Y)
End If

and then check if my cursor touched something..

B4X:
If sprCursor.BoundingRectangle.overlaps(sprMusic.BoundingRectangle) Then
'Cursor touched something......
End If

B4X:
Sub TranslateCoordinates(X As Int, Y As Int) As lgMathVector2
    Dim p As lgMathVector2
    p.X = vpWidth * (X / lGdx.Graphics.Width)
    p.Y = vpHeight - (vpHeight * (Y / lGdx.Graphics.Height)) 'flip Y coords
    Return p
End Sub

Please note that sprCursor and sprMusic are both lgSprite objects. :)

I understand your frustration when you ask a simple question, and people always go around the bush to give you a simple answer, on the other hand i also suggest you read the tutorials, for something like this i would suggest you go with lgSCN2D, you can declare your balloons actors, each actor will have it's own touch event, check out my whack a beiber game.

Good luck.
 
Upvote 0

abhishek007p

Active Member
Licensed User
Longtime User
I understand your frustration when you ask a simple question, and people always go around the bush to give you a simple answer, on the other hand i also suggest you read the tutorials, for something like this i would suggest you go with lgSCN2D, you can declare your balloons actors, each actor will have it's own touch event, check out my whack a beiber game.

Good luck.

hehe.. already solved it.. thanks,
 
Upvote 0
Top