Moshe
Moshe

Reputation: 58097

Get touch location from touchesBegan? (And other game questions)

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?

Upvotes: 7

Views: 10356

Answers (1)

Jim Buck
Jim Buck

Reputation: 20724

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'
}

Upvotes: 17

Related Questions