저는 직소 응용 프로그램을 만들고 있습니다. 각 조각을 만졌을 때 올바른 위치에 넣으면 bool (droppedInPlace
)에 YES
이 지정되고 잘못된 위치에 놓인 경우 NO
이 할당됩니다. 그런 다음 40 개의 bool (퍼즐 조각 수) 배열을 만들어야한다고 생각합니다. 각 항목은 처음에는 NO로 설정됩니다.bool 배열 만들기 및 반복하기
이 bool (droppedInPlace
)은 replaceObjectAtIndex
을 사용하여 배열에 삽입됩니다. 그런 다음이 배열을 반복하고 싶습니다. 모든 항목이 참/예/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];
}
}
그리고 체크 기능 : 당신의 piecesInPlace
배열 방법 checkJigsawCompleted
에서 초기화되지 않고 다음 줄에 충돌합니다
-(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
}
나는 실제로 내 스토리 보드에서 1-40 개의 모든 이미지 태그를 부여했습니다. 'replaceObjectAtIndex : (tag-1)'을 사용하는 것이 맞습니까? 그 괄호는 -1 색인에 액세스하려고하지 않을 것입니까? – garethdn
생각해보십시오. 첫 번째 태그가 0 인 경우 괄호로 인해 태그 -1이 -1이되지 않도록하려면 어떻게해야합니까? 물론 IB에서 수동으로 태그를 설정하는 경우 1에서 가장 낮은 태그를 시작하면됩니다. – jmstone617
미안하지만, 우리가 전선을 가로 지른 것 같아요. 내 가장 낮은 태그는 실제로 1입니다. – garethdn