2012-04-04 5 views
-2

저는 직소 응용 프로그램을 만들고 있습니다. 각 조각을 만졌을 때 올바른 위치에 넣으면 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

당신이 원하는 배열을 반복하는 법을 아십니까? 그래서 -enumerateObjectsUsingBlock:

__block jigsawIsComplete = true; 
[piecesInPlace enumerateObjectsUsingBlock: ^(id obj, NSUInteger i, BOOL* stop) 
    { 
     jigsawIsComplete = jigSawIsComplete && [obj boolValue]; 
     *stop = !jigsawIsComplete; 
    }]; 
2

참고 다음은 내 코드입니다.

그것이 올바른 값으로 초기화됩니다 가정, 모든 값에 해당하는 경우를 확인하는 방법은 간단하다

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

return true; 

편집 : 또는 더 나은 아직 :

return ![piecesInPlace containsObject:[NSNumber numberWithBool:NO]]; 
1

, 몇 가지 : 당신은 for(;;) 루프 또는 for(.. in ..) 루프 또는, 이런 종류의 물건에 대한 내 즐겨 찾기를 사용할 수 있습니다. 우선 퍼즐 조각의 태그를 기반으로이 조각 안쪽 배열을 대체하는 것입니다. 하지만 이러한 뷰의 태그는 설정하지 않았습니다. 그래서,이 라인 : 더 나쁜 무엇

int tag = touchedView.tag; 

항상 0이됩니다, 당신 replaceObjectAtIndex : 태그-1 비트가이 시간에하지 않은 배열 (의 -1 인덱스를 액세스하려고하지만 것 Apple이 일부 변경 사항을 공개하면 합법적입니다. 당신이 값을 찾을 일단

가 지금까지처럼 배열을 반복하고 진정한 가치를 확인 , 나는이 방법을 사용

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

을 사용하는 것이 좋습니다 것입니다, 당신은 YES로 정지 포인터를 설정할 수 있습니다 귀하의 배열에 열거가 끝납니다. stop이 NO 인 동안에 만 계속 열거하려고합니다. 멈추지 않고 배열을 통해 만들면 모든 값은 YES가됩니다.

+0

나는 실제로 내 스토리 보드에서 1-40 개의 모든 이미지 태그를 부여했습니다. 'replaceObjectAtIndex : (tag-1)'을 사용하는 것이 맞습니까? 그 괄호는 -1 색인에 액세스하려고하지 않을 것입니까? – garethdn

+0

생각해보십시오. 첫 번째 태그가 0 인 경우 괄호로 인해 태그 -1이 -1이되지 않도록하려면 어떻게해야합니까? 물론 IB에서 수동으로 태그를 설정하는 경우 1에서 가장 낮은 태그를 시작하면됩니다. – jmstone617

+0

미안하지만, 우리가 전선을 가로 지른 것 같아요. 내 가장 낮은 태그는 실제로 1입니다. – garethdn

관련 문제