Virtual Stick

Monkey Forums/Monkey Code/Virtual Stick

Samah(Posted 2011) [#1]
One of the staple components of many touchscreen games is the so-called "virtual stick". Basically it's a small circle that you touch and move your thumb around in a similar manner to an analog stick on a game controller.

Here's my take on it, though it's mostly a work-in-progress. If you see any glaring errors, please fix and/or let me know. ;)

The test app is ugly, but the VirtualStick class is pretty easy to use.
If you're testing it on Android, it'd be best to force landscape and fullscreen.




Raz(Posted 2011) [#2]
Nice one thank you :) This will be very handy for me soonish.


pinete(Posted 2011) [#3]
useful!!! :) thank you very much!


therevills(Posted 2011) [#4]
Cool :)

Im not a fan of this sort of controls as it blocks the screen (with the control and your fingers), but I realise in some games you need to do it this way on a touch screen device...

Oh BTW why do you loop thru all 32 touch events?
			for local i:int = 0 to 31
				if TouchHit(i) then
					touching[i] = true
					mystick.StartTouch(TouchX(i), TouchY(i), i)
				elseif TouchDown(i) then
					mystick.UpdateTouch(TouchX(i), TouchY(i), i)
				elseif touching[i] then
					touching[i] = false
					mystick.StopTouch(i)
				end
			end




Samah(Posted 2011) [#5]
Because I'm a noob and don't know how the touch system works yet. :)
What's the best way to check for a touch event in a given area? I can't guarantee it'll be the first touch event.

I threw that code together really quickly, so I realise now I can make it check only the relevant touch event once it's been started.

Updated version:




Virtech(Posted 2011) [#6]
@Samah

Could you possibly update your example (or add one) that show how to create 2 virtual sticks? Thanks


Samah(Posted 2011) [#7]
@Virtech
I'm currently working on an app that uses two sticks, and I'm cleaning up/enhancing the VirtualStick class as I go. Once I'm happy with it I'll post the updated code with an example on using any number of sticks.

It's sorta hard for me to test multiple sticks at the moment since the Nexus One's multitouch is so damned awful. Once the Xoom comes out (early next month hopefully) I'm ordering a Nexus S with it.


Virtech(Posted 2011) [#8]
Cool! Looking forward to it :)

Edit: Multitouch on my samsung galaxy s works pretty good. I can test dual stick support on my phone if you wish.


Perturbatio(Posted 2011) [#9]
This looks useful. Thanks :)


Loofadawg(Posted 2011) [#10]
Works great on my Captivate !


xzess(Posted 2011) [#11]
superb thanks very much for sharing!


dopeyrulz(Posted 2011) [#12]
Samah,

This works great on Windows Phone 7 - thanks for sharing. Would be be possible to implement an old school 4/8 way stick with this process?

I've sorta hacked in a check based on the angles but something where it would lock directly into position (on the 8 axis of the compass) would be great.


Samah(Posted 2011) [#13]
I'm going to clean this up a bit at some point and check it in to the Diddy project.


dopeyrulz(Posted 2011) [#14]
Ah - ok. Sounds good. Thanks again for the code.


NoOdle(Posted 2011) [#15]
Thanks, this works great on the iPhone as well :)


Neuro(Posted 2012) [#16]
This code actually works great but one issue i noticed on IOS target, that if you have 2 virtual joysticks on screen, the second one won't activate if the first joystick is touched first. However, if the second joystick is touched then both of them will actually work.

Oddly enough, this does NOT occur in HTML5 (when running on an IOS browser) though, not sure whats causing it to do this in IOS.

EDIT : nevermind, figured out the problem :)


Paul - Taiphoz(Posted 2012) [#17]
I was also wondering about the looping through the touch events, as I understand it the first touch event is assumed to be the player 1 finger and gets treated as such.

So you would I think be able to drop that loop and just look for touch 1, unless your planning duel sticks, then it would be touch 1 and 2, touch two being called into play as soon as a second finger touches the screen at the same time.

I might be wrong tho, like you iv not really played with touch much at all. but all that being said I'm glad your working this into Diddy because its something I have on my todo list for Terminal 2, so you doing it now is gona save me tons of time.


thanks. and nice work.


therevills(Posted 2012) [#18]
EDIT : nevermind, figured out the problem :)

What was the issue and how did you fix it?


Neuro(Posted 2012) [#19]
What was the issue and how did you fix it?

Wasn't really an issue, it was me stupidly forgetting to also filter by the IOS target on the second virtual joystick which is why it never activated :).