2014-06-11 2 views
0
-(id) init 
    { 
     if((self=[super init])) { 
     _targets = [[NSMutableArray alloc] init]; 
     temp = [NSArray arrayWithObjects:@"1", @"2", @"3", @"4",nil]; 
     tempArray = [[NSMutableArray alloc] initWithArray:temp]; 
     resultArray = [[NSMutableArray alloc] init]; 
     [self thelogic]; 
     } 
    return self; 
    } 

    -(void)thelogic 
    { 
     int i; 
     int count = 4; 
     for (i = 0; i < 3; i ++) { 
     int index = arc4random() % (count - i); 
     [resultArray addObject:[tempArray objectAtIndex:index]]; 
     CCSprite *target = [CCSprite spriteWithFile:[NSString stringWithFormat:@"%@.png", [tempArray objectAtIndex:index]]]; 
     target.tag = [[NSString stringWithFormat:@"%@",[tempArray objectAtIndex:index]] integerValue]; 
     [self addChild:target]; 
     [_targets addObject:target]; 
     [tempArray removeObjectAtIndex:index]; 
    } 
    } 

    -(void)ccTouchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{ 
     [_targets removeAllObjects]; 
     [self thelogic]; 
    } 

, 나는 세 가지 다른 번호를 얻을이 장면을 입력 할 때 서로 다른 이미지와 세 개의 스프라이트를 만들고, 나는 세 이전 스프라이트가 제거됩니다, 화면을 터치 할, 그리고 3 개의 새로운 스프라이트가 표시되지만, 업 코드는 항상 충돌하므로 어떻게 수정해야합니까? 감사에러 인덱스 위 코드에서

+0

응용 프로그램이 충돌 할 때 정확한 오류 메시지를 게시하십시오. – sergio

+0

@ 오류 오류 메시지는 '*** - [NSArrayM objectAtIndex :] : 경계 3 초과 [0 .. 0]', 감사합니다 – matt

답변

1

, 그것은 당신이 당신의 theLogic 방법에 뭘 하려는지 이해하기 어렵다,하지만 난 당신이 충돌을 해결합니다

int index = arc4random() % (count - i); 

int index = arc4random() % [tempArray count]; 

이를 대체하는 제안하지만, 귀하의 프로그램이 예상대로 작동하지 않을지 의심 스럽습니다. 사실 tempArray 만 초기화 메소드에 채 웁니다. theLogic에 대한 첫 번째 호출은 해당 요소를 모두 제거하고 배열이 더 이상 채워지지 않는 것을 봅니다. 따라서 ccTouchesBegan을 호출 한 다음 theLogic을 호출하면 tempArray가 비어있게됩니다.

죄송하지만 포인트가 누락되었습니다.

+0

에 있습니다. 'thelogic', 3 개의 스프라이트를 추가하려고합니다. 각 스프라이트에는 다른 태그와 다른 이미지가 있습니다. – matt

+0

@DMusic : 귀하의 의견에 감사드립니다. 'index'에 대한 제 제안이 충돌을 수정했는지 확인할 수 있습니까? – sergio