-2

I'm creating a jigsaw application. When each piece is touched, the idea is that a bool (droppedInPlace) is assigned YES if it is put in its correct place, and NO if it is dropped in the wrong place. I then think I need to create an array of 40 bools (the number of jigsaw pieces), each of these entries will be initially set to NO.

This bool (droppedInPlace) is then inserted into an array using replaceObjectAtIndex. I then want to loop through this array. If all entries are true/yes/1, then the jigsaw is completed and i'll run some code.

I'm having some trouble doing this. I can't figure out how to code my idea above. Below is my code:

-(void) touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
UITouch *touch = [touches anyObject];
UIView *touchedView = [touch view];
// filename to be read from
NSString *filePath = [[NSBundle mainBundle]
                      pathForResource: 
                      @"jigsawHomeCenterPoints" 
                      ofType:@"plist"];
// array of cgpoints in string format
NSArray *jigsawHomeCentersArray = [NSArray arrayWithContentsOfFile:filePath];
// the tag number of the selected image
int tag = touchedView.tag;
NSLog(@"tag: %i", tag);
// says that the piece has not been dropped in it's holder
bool droppedInPlace = NO;
// if what has been touched is part of the array of images
if ([imagesJigsawPieces indexOfObject:touchedView] != NSNotFound) {
    // preparing new snap-to-center position
    // using (tag-1) as an index in the HomeCentersArray corresponds to where the image should be placed
    // this was a conscious decision to make the process easier
    CGPoint newcenter = CGPointFromString([jigsawHomeCentersArray objectAtIndex:tag-1]);
    // the location of the current touch
    CGPoint location = [touch locationInView:self.view];
    // call the animate method upon release
    [self animateReleaseTouch:touchedView withLocation:location];
    // setting a proximity range - if it is within this 80x80 area the image will snap to it's corresponding holder (HomeCenter)
    if (location.x > newcenter.x-40 && location.x < newcenter.x+40 
        && location.y > newcenter.y-40 && location.y < newcenter.y+40) {
        touchedView.center = newcenter;
        touchedView.alpha = 1.0;
        droppedInPlace = YES;
    } else {
        touchedView.alpha = 0.5;
        droppedInPlace = NO;
    }
    NSLog(@"True or false: %i", droppedInPlace);
[self checkJigsawCompleted:droppedInPlace withTag:tag];
}
}

And the check function:

-(void) checkJigsawCompleted:(BOOL)inPlace withTag:(NSInteger)tag {
NSMutableArray *piecesInPlace;
[piecesInPlace replaceObjectAtIndex:tag-1 withObject:[NSNumber numberWithBool:inPlace]];
//code to loop through array - check if all entries are true/yes/1
//if so, jigsaw is completed - run some other code
//else, do nothing
}
4

3 回答 3

2

请注意,您的piecesInPlace数组未在方法中初始化,checkJigsawCompleted将在下一行崩溃。

假设它用正确的值初始化,检查所有值是否为真的方法很简单:

for (NSNumber *boolNumber in piecesInPlace) {
    if (![boolNumber boolValue])
       return false;
}

return true;

编辑:或者更好:

return ![piecesInPlace containsObject:[NSNumber numberWithBool:NO]];
于 2012-04-04T10:57:57.640 回答
1

所以,有几件事。首先,您要根据拼图的标签替换这个piecesInPlace 可变数组中的对象,看起来像这样。但是,您永远不会设置这些视图的标签。所以,这一行:

int tag = touchedView.tag;

将始终为 0。更糟糕的是,您的 replaceObjectAtIndex:tag-1 位将尝试访问不合法的数组的 -1 索引(此时,但一旦 Apple 公开了一些更改)合法。

至于只是循环遍历数组并检查真值,我建议使用该方法

- (void)enumerateObjectsUsingBlock:(void (^)(id obj, NSUInteger idx, BOOL *stop))block

使用它,一旦在数组中找到 NO 值,您就可以将停止指针设置为 YES 并且枚举将结束。只要停止为否,您只想继续枚举。如果你不停地通过你的数组,所有的值都是YES。

于 2012-04-04T11:03:50.740 回答
1

您想知道如何遍历数组吗?您可以使用for(;;)循环或for(.. in ..)循环,或者我最喜欢的这类事情: -enumerateObjectsUsingBlock:

__block jigsawIsComplete = true;
[piecesInPlace enumerateObjectsUsingBlock: ^(id obj, NSUInteger i, BOOL* stop)
    {
        jigsawIsComplete = jigSawIsComplete && [obj boolValue];
        *stop = !jigsawIsComplete;
    }];
于 2012-04-04T10:59:04.060 回答