I am creating a game using GLKit (not using Cocos2d) and I basically need two "buttons" - each button 250 points wide and placed on opposite sides in landscape mode. MultipleTouch is YES as I need to track more than one touch.
In touchesbegan I determine if the touch.x <= 250 (Button 1) or >= view.bounds.size.width - 250 (Button 2). Based on this I set a BOOL to YES to determine state.
In touchesended I want to know when the user is no longer "pressing" Button 1 or Button 2. The thing is - I cannot just test where the touch happened in the view any longer as the user could have moved the finger so it is no longer over the "Button" that was initially pressed. That means if the user started the touch by pressing Button 1 but moved the finger outside the Button boundaries and then lifted the finger - the state is still YES.
Is there a good way to reliably track where the touch first started in TouchesEnded? Any pointer to source code would be highly appreciated.
Edit: Here is the code I have created:
Please note that I know this code is wrong as it only tracks one touch. However, what I want to achieve is when the user presses the left button and moves the finder out of that button .. I need to know that the touch in touchesEnded is equivalent of the touch that started in the button earlier on so that I know that button is no longer "pressed".
- (void) touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
UITouch *touch = [touches anyObject];
// Determine where the touch happened
CGPoint p = [touch locationInView:self.view];
// If the user pressed the left gutter then break
if ( p.x <= 250 )
isBreaking = YES;
// If the user pressed the right gutter then accelerate
if ( p.x >= (self.view.bounds.size.width - 250) )
isAccelerating = YES;
}
- (void) touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
UITouch *touch = [touches anyObject];
// Determine where the touch happened
CGPoint p = [touch locationInView:self.view];
// If the user pressed the left gutter then stop break
if ( p.x <= 250 )
isBreaking = NO;
// If the user pressed the right gutter then stop accelerate
if ( p.x >= (self.view.bounds.size.width - 250) )
isAccelerating = NO;
}