Going touch position to do things with.

AntonBrz

Member
Licensed User
Longtime User
How do I grab the X and Y positions of where a finger touches the screen in a repeating loop? I've set up a panel, with the "touch" option checked in the Designer/Generate Members, so that now the code has a sub area:


Sub Panel1_Touch (Action As Int, X As Float, Y As Float)

End Sub

As a newbie and not the brightest programmer on the block, I've read posts on the touch subject, and the code examples seem so complicated I don't know what I need to include. is there a simple way to read the X and Y when my finger touches the screen so I can write something like 'If X is >100 AND X<110 And Y>60 AND Y<70 THEN do stuff?' I know that isn't written correctly, its just to explain I'm looking to see if the user is touching a certain area of the screen.

I want to write the simplest, ie shortest code. Thanks.

:sign0163:
 

stevel05

Expert
Licensed User
Longtime User
You can't look to see if the user is touching the screen. It's event driven, like most other things in Android. The touch sub will be called when the object is touched, the touch moves, or is released. The state of the variable Action tells you which of the three caused the event. (sorry I can't remember which action is which, I'm not at my PC but it is in the documentation.)

Hope this helps.

-------------------
Sent via Tapatalk
 
Upvote 0

AntonBrz

Member
Licensed User
Longtime User
I just need the X and Y

You can't look to see if the user is touching the screen. It's event driven, like most other things in Android. The touch sub will be called when the object is touched, the touch moves, or is released. The state of the variable Action tells you which of the three caused the event. (sorry I can't remember which action is which, I'm not at my PC but it is in the documentation.)

Hope this helps.

-------------------
Sent via Tapatalk

Yes, you're all helping, and I'm grateful. But are you saying there is no way to read the X and Y of the touched spot in B4A code? What about the GESTURES library perhaps? How are people making games where different parts of an image are touched?

Thank you for any response.
 
Upvote 0

stevel05

Expert
Licensed User
Longtime User
As thedesolatesoul says, the x,y values are available in the touch subroutine. If you want them to persist, you can copy them to global variables.

If you want multi-touch, then yes, you need to look at the gestures library.

-------------------
Sent via Tapatalk
 
Upvote 0

stevel05

Expert
Licensed User
Longtime User
Have a look at one of the example games Erel has posted on the forum. They should give you more information.

-------------------
Sent via Tapatalk
 
Upvote 0

AntonBrz

Member
Licensed User
Longtime User
Dawn breaks on XY problem

Ohhhh.... That's all there is to it? In apology, I never used anything but buttons before to get responses from the user. I feel like the guy in a cartoon with the light bulb over his head. In other words, if I've already dimmed say GameX and GameY as Int, I can pass the x and y to them within the touch sub. Thank you. I feel of course like an ignoramus for not understanding what was being said. I'm very thankful to you and the others answering.
 
Upvote 0

AntonBrz

Member
Licensed User
Longtime User
Erel's Games with Touch

Have a look at one of the example games Erel has posted on the forum. They should give you more information.

Excuse me. I'm still having problems. I can find Erel's posts but looking through their list is like searching through the Encyclopedia Britannica. Could please post a link to Erel's games? Or the simplest one using touch position reading?
 
Upvote 0

AntonBrz

Member
Licensed User
Longtime User
Something is working!

Aha! It is working, and reading the X (and probably the Y) For a test I put a label on top of the panel and set it to display the position of where I touch the screen. Now I should be able to use the X and Y to trigger things happening when a certain part of the image is touched. Now if I can just change X into a percentage, I should be able to make it work on different sized Android screens.
 
Upvote 0
Top