views:

29

answers:

1

I'm trying to get the location of the touch event on the screen from

-(void) touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event.

What code can I use to read the touch coordinates? I've gotten this far so far:

NSLog(@"Touch data: %@", [NSString stringWithFormat:@"NSSet: %@",[touches description]]);

I'm trying to make a game using an OpenGL template application. The touches will only register in the EAGLView file, not the ESRenderer1 file. I assume that I should use the EAGlView to set variables and then read the variables from there into the ESRenderer to perform game calculations. Is this a good approach?

+1  A: 

The following code does it:

NSArray *touchesArray = [touches allObjects];
for(int i=0; i<[touchesArray count]; i++)
{
    UITouch *touch = (UITouch *)[touchesArray objectAtIndex:i];
    CGPoint point = [touch locationInView:nil];

    // do something with 'point'
}
Jim Buck
I'd choose to use `for (UITouch *touch in touches) {/*...*/}` instead. (Fast enumeration!)
bddckr
What if I just want the latest touch, none of the other information? How would this work?
Moshe
I think that array is a list of the "latest" touches in that it represents where finger(s) currently are on the screen. Most of the time, the array will only have one element for most applications.
Jim Buck