- Home /
A different take on multiple touches
Hi there,
I'm trying to create a simple input system for a 2D platformer that basically takes the rightmost 2/3 of the screen for touch input/swipes, and the leftmost 1/3 part for a simple JoyPad that spawns where the user touch.
So, if the user touch the left part of the screen, I need to spawn the joystick, and keep it alive even if he slide his finger over the right part of the screen. On the other hand, I have to compute his right screen swipes, even when it exceeds the left area. I'm trying to achieve that in a way that the input system doesn't get confused with different inputs, and generate swipes with the joy touch, or vice-versa.
I did my best (which is not very much) and couldn't figure out a safe way to do this without bugs and glitches. The "Penelope" input solution didn't cut it as well (it threads the joy as a fixed gui element, and mine is dynamically spawned).
The relevante part of the code is my "GetTouchOutput" function, which runs on Update(), but there are functions used on the example below (not directly relevant, but I can post it if necessary). setTouchMotion just computes a formated touch output to be used by the game, and joyDraw / touchDraw are responsible for designing the gui elements on the fly. The touchZone is Rect(Screen.width - touchSize,0, Screen.width, Screen.height).
Any help will be most appreciated! Thanks in advance.
function getTouchOutput () {
var i : int = 0;
var j : int = 0;
var count : int = Input.touchCount;
if (count > 0) {
while(i < count) {
if (!touchStartSet[i]) {
touch[i] = Input.GetTouch(i);
touchFinger[i] = touch[i].fingerId;
touchStart[i] = touchCurrent[i] = touch[i].position;
touchStartSet[i] = true;
if (touchZone.Contains(touchStart[i])) touchPad[i] = true;
else touchPad[i] = false;
}
else {
j = touchFinger[i];
touch[j] = Input.GetTouch(i);
if (touchPad[j]) touchDraw(touchPrevious[j], touchCurrent[j]);
else joyDraw(touch[j].position, touchStart[j]);
touchPrevious[j] = touchCurrent[j];
if (touch[j].position != touchPrevious[j]) touchCurrent[j] = touch[j].position;
touchOutput[j] = setTouchMotion(touchPrevious[j], touchCurrent[j]);
if (touch[j].phase == TouchPhase.Ended || touch[j].phase == TouchPhase.Canceled )
touchStartSet[j] = false;
}
i++;
}
}
}
function Update() {
getTouchOutput ();
}