2012-03-28 5 views
0

에서 점수 레이블을 어떻게 증가시켜 레이블을 만들 수 있으며 이미지가 다른 이미지와 충돌하면 10 점이 증가합니다. 점수 = 점수 +10; 하지만이 대신 40의 증가 이유를 모르겠습니다. 그래서 여기 내 코드는 다음과 같습니다에 대한cocos2d

-(id) init 
{ 
    if((self=[super init])) { 

     [self schedule:@selector(update:)]; 

     score = 0; 

     scoreLabel = [CCLabelTTF labelWithString:[NSString stringWithFormat:@"%d",score] fontName:@"PUSAB___.TTF" fontSize:15 ]; 
     scoreLabel.position=ccp(450,30); 
     [self addChild:scoreLabel]; 
    } 
} 

- (void)update:(ccTime)dt { 
    if (CGRectIntersectsRect(mangeurRect, targetRect)) { 
     [targetsToDelete addObject:target];  
     score=score + 10; ;// Not really, but your score changes somehow... 
     [scoreLabel setString: [NSString stringWithFormat:@"%d",score]]; 
    }      
} 

미안 내 영어 나 프랑스어 해요 :/

+2

영어 실력은 괜찮습니다. 그러나 코드 서식 지정 ... :) – Almo

+0

lol !! 그래서 문제는 무엇입니까 ?? :) –

+0

"mangeurRect"및 "targetRect"는 어떻게 초기화합니까? 모든 프레임 또는 상수 로컬 스프라이트 바운딩 박스를 업데이트해야하는 화면 관련 rect입니까? –

답변

1

CGRectIntersectsRect에 들어갈 프레임이 없다고 생각합니다. 당신은 아래 코드

- (void)update:(ccTime)dt { 

if (CGRectIntersectsRect([mangeurRect frame], [targetRect frame])) { 
    [targetsToDelete addObject:target];  
    score += 10; 
    [scoreLabel setString: [NSString stringWithFormat:@"%d",score]]; 
}      

} 이미지가 서로 충돌 할 때마다 당신은 아마 점수를 얻을 것 이로써

또는

- (void)update:(ccTime)dt { 

if (CGRectIntersectsRect([mangeurRect boundingBox], [targetRect boundingBox])) { 
    [targetsToDelete addObject:target];  
    score += 10; 
    [scoreLabel setString: [NSString stringWithFormat:@"%d",score]]; 
}      

}

증가를 시도해야합니다.

0

당신은 아마 당신의 목표는 실제로 경우에, 과정을 다음과 같이 삭제 대상에없는 여부를 확인해야합니다 targetsToDelete 구조가 어떻게 든 몇 진드기 지연 웁니다 :

- (void)update:(ccTime)dt { 
    if ([targetsToDelete containsObject:target]) return; // already scored. 
    if (CGRectIntersectsRect(mangeurRect, targetRect)) { 
     [targetsToDelete addObject:target];  
     score=score + 10; ;// Not really, but your score changes somehow... 
     [scoreLabel setString: [NSString stringWithFormat:@"%d",score]]; 
    }      
} 

위 targetsToDelete가있는 NSMutableArray 있다고 가정합니다.

+0

아니요 아직 여기에 문제가 있습니다 : ( –

+1

코드에 로그를 추가하고 검사중인 개체를 기반으로 점수가 변경된시기를 확인하십시오. – Setrio

0

업데이트 방법이 완전하지 않습니다. 이 시도 :

- (void)update:(ccTime)dt { 
    if (CGRectIntersectsRect(mangeurRect, targetRect)) { 
     [targetsToDelete addObject:target];  
     score=score + 10; ;// Not really, but your score changes somehow... 
     [scoreLabel setString: [NSString stringWithFormat:@"%d",score]]; 

     //do something with your targetsToDelete array.. 
     for (CCSprite *target in targetsToDelete) { 
     //[_targets removeObject:target]; //uncomment this line, if you have saved your targets in a _targets array 
     [self removeChild:target cleanup:YES];         
     } 
    }      
} 

참조 : http://www.raywenderlich.com/352/how-to-make-a-simple-iphone-game-with-cocos2d-tutorial