【发布时间】:2012-04-18 01:43:34
【问题描述】:
我正在创建一个拼图应用程序。当每块被触摸时,如果将其放在正确的位置,则将布尔 (droppedInPlace) 分配为 YES,如果将其放在错误的位置,则分配 NO。然后我想我需要创建一个包含 40 个布尔值(拼图块的数量)的数组,这些条目中的每一个最初都将设置为 NO。
然后使用replaceObjectAtIndex 将这个布尔值 (droppedInPlace) 插入到一个数组中。然后我想遍历这个数组。如果所有条目都是 true/yes/1,那么拼图就完成了,我将运行一些代码。
我在执行此操作时遇到了一些问题。我不知道如何在上面编写我的想法。以下是我的代码:
-(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];
}
}
还有校验功能:
-(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
}
【问题讨论】:
标签: objective-c ios arrays boolean