I’m working on a simple game where “particles” are attracted to user touches.
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
NSArray *touch = [touches allObjects];
for (numberOfTouches = 0; numberOfTouches < [touch count]; numberOfTouches++) {
lastTouches[numberOfTouches] = [((UITouch *)[touch objectAtIndex:numberOfTouches]) locationInView:self];
}
isTouching = YES;
}
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
NSArray *touch = [touches allObjects];
for (numberOfTouches = 0; numberOfTouches < [touch count]; numberOfTouches++) {
lastTouches[numberOfTouches] = [((UITouch *)[touch objectAtIndex:numberOfTouches]) locationInView:self];
}
}
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
NSArray *touch = [touches allObjects];
for (numberOfTouches = 0; numberOfTouches < [touch count]; numberOfTouches++) {
lastTouches[numberOfTouches] = [((UITouch *)[touch objectAtIndex:numberOfTouches]) locationInView:self];
}
if (!stickyFingers) {
isTouching = NO;
}
}
lastTouches is an array of CGPoints that another part of the program uses to move the particles.
The problem I am having is that the way I have it setup now, whenever any of the 3 functions are called, they overwrite the array of CGPoints and the numberOfTouches. I didn’t think this would be a problem, but it turns out that TouchesMoved only gets the touches that have changed and doesn’t get the touches that have stayed the same. As a result, if you move one of your fingers but not another, the program forgets about the finger that isn’t moving and all the particles go towards the moving finger. If you move both fingers, the particles move in between the two fingers as they should.
I need someway to hold on to the touches that haven’t moved while updating the ones that have.
Any Suggestions?
Set theory to the rescue!
In the above,
touchesThatHaveNotMovedwill hold the touches that didn’t move in the lasttouchesMoved:, andtouchesThatHaveNeverMovedwill hold the touches that have not moved even once since they began. You can omit either or both variables, and all the statements that involve them, that you don’t care about.