iDevGames Forums
Advice on coding of On-Screen Controls ( Joystick and multiple buttons) - Printable Version

+- iDevGames Forums (http://www.idevgames.com/forums)
+-- Forum: Development Zone (/forum-3.html)
+--- Forum: Game Programming Fundamentals (/forum-7.html)
+--- Thread: Advice on coding of On-Screen Controls ( Joystick and multiple buttons) (/thread-878.html)



Advice on coding of On-Screen Controls ( Joystick and multiple buttons) - Elphaba - Aug 9, 2009 07:30 AM

Hi

I'm finally getting somewhere now! Wink

I'd just like to know if there are any good tutorials or advice or even code snippets on how best to handle on screen controls and multiple buttons presses / releases.

The way I've implemented it, is that I've abstracted out of the EAGLView the touch events and they are being passed down a chain to an object that is just concerned with making 'sense' of them.

I have the requirement for a joystick (say, D-Pad) on the left of the screen and three buttons on the right.

The three buttons can be pressed down at the same time as one or more of the D-Pad buttons, however, no more than one of the buttons on the right can be down at the same time.

Also, no more that 2 of the D-Pad buttons can be down at the same time, and as ofcourse, with a D-Pad I must expect the user to drag/move their finger from one part of the buttons to another and transition accurately between these 'states'

At the moment I've got the 'touchesBegan:touches blah blah' event passing the entire NSSet * of touches down to my delegate, so I can get the 'full' list of touch events.

But how do I seperate them out and make sense of it all?

My first attempt, was just to get the current position of the touch and make a basic ( if touch.x > button1.x and touch.x < button1.x + button1.width) blah
blah blah for all the buttons,

but I scrapped that because it can't handle all the 'semantics' of what I need.


Can anyone please advise?

Thanks so much.

Elphaba


Advice on coding of On-Screen Controls ( Joystick and multiple buttons) - Elphaba - Aug 10, 2009 11:16 AM

*bump*

Anyone?


Advice on coding of On-Screen Controls ( Joystick and multiple buttons) - ThemsAllTook - Aug 10, 2009 11:53 AM

I'd approach it like this:
  • When a new touch begins, do rough partitioning to determine whether it's in the d-pad area, the button area, or elsewhere (probably with a simple 2D squared distance test).
  • If in d-pad area:
    • If there's already d-pad touch being tracked, ignore it.
    • If not, partition the d-pad into 8 equal wedge-shaped regions and determine which one the touch is in, where the regions mean "left", "left-down", "down", etc. Compute this again each time this touch moves.
  • If in the button area:
    • Determine which button was pressed with some sort of partitioning that makes sense for your button layout. It's possible you'll want larger hit areas than the buttons themselves, but I'd have to play with it to know for sure.
    • Regardless of where this touch moves, the button is "down" while the touch is held, and "up" once it's released.



Advice on coding of On-Screen Controls ( Joystick and multiple buttons) - Elphaba - Aug 10, 2009 07:15 PM

Sorry Thems...

That doesn't help.

Knowing if the touch happened in an area is not the problem.

The problem is with the standard event handlers as I mentioned and handling the 'multi touch ' and a touch MOVED / END on the D-PAD at the same time another touch Down/Up is happening on a button.

THAT'S the hard part -and one I can't find any help on.


Advice on coding of On-Screen Controls ( Joystick and multiple buttons) - Bachus - Aug 10, 2009 07:46 PM

Something like this?

Code:
- (void) touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    NSEnumerator        *enumerator;
    UITouch            *touch;
    CGPoint            location;

    enumerator = [touches objectEnumerator];
        
    while (touch = [enumerator nextObject])
    {
        location = [touch locationInView:self];
                
        if (location is inside of button rect)
            // Set button down/activated/whatever
        else if... // yadda yadda yadda
    }
}

touchesMoved:withEvent: is basically the same and touchesEnded:withEvent is the same, but in reverse (buttons deactivating, etc). You can test against the touch's previous location with...

Code:
CGPoint        oldLocation = [touch previousLocationInView:self];

... to see if a touch has left a button's rect without being released.


Advice on coding of On-Screen Controls ( Joystick and multiple buttons) - Elphaba - Aug 10, 2009 11:02 PM

Bachus Wrote:Something like this?

Yeah, kinda. Thanks Bachus.

That's kinda what I've already got, except I'm using the '
Code:
for ( UITouch * touch in touches)
{
     CGPoint point = [touch locationInView:receivingView];
     {
         [self callTestMethod:point];
     }
}
way of doing things..

The problem I'm having - and have had all this time is with the 'touchesMoved' method.

Example:

If I move my finger over the D-Pad, I want to track the movement and operate upto 2 adjacent sectors, i.e. Left and Up.. okay?

BUT if I move my finger from say the 'up' position, and move it OFF of the on-screen D-Pad, then I need to 'release' the D-Pad.

Same goes for the 3 buttons on the right hand side. If I press them, and then move / drag my finger up and off of them, but still in contact with the screen, they 'stay pressed' and not released.

This is my big mental block.

Thanks