开发者

TouchesMoved only returns touches that have changed ignoring touches that haven't changed

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[number开发者_JAVA技巧OfTouches] = [((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!

//Instance variables
NSMutableSet *allTheTouches;
NSMutableSet *touchesThatHaveNotMoved;
NSMutableSet *touchesThatHaveNeverMoved;

//In touchesBegan:withEvent:
if (!allTheTouches) {
    allTheTouches = [[NSMutableSet alloc] init];
    touchesThatHaveNotMoved = [[NSMutableSet alloc] init];
    touchesThatHaveNeverMoved = [[NSMutableSet alloc] init];
}
[allTheTouches unionSet:touches];
[touchesThatHaveNotMoved unionSet:touches];
[touchesThatHaveNeverMoved unionSet:touches];

//In touchesMoved:withEvent:
[touchesThatHaveNeverMoved minusSet:touches];
[touchesThatHaveNotMoved setSet:allTheTouches];
[touchesThatHaveNotMoved minusSet:touches];

//In touchesEnded:withEvent:
[allTheTouches minusSet:touches];
if ([allTheTouches count] == 0) {
    [allTheTouches release]; //Omit if using ARC
    allTheTouches = nil;
    [touchesThatHaveNotMoved release]; //Omit if using ARC
    touchesThatHaveNotMoved = nil;
}
[touchesThatHaveNotMoved minusSet:touches];
[touchesThatHaveNeverMoved minusSet:touches];

In the above, touchesThatHaveNotMoved will hold the touches that didn't move in the last touchesMoved:, and touchesThatHaveNeverMoved will 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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜