2011-11-19 2 views
2

아이폰 용 cocos2d에서 게임을 시작하기위한 카운트 다운 타이머를 구현하는 방법을 보여 주실 수 있습니까?카운트 다운 카운터/타이머

내가 말하는 것은 "재생"을 누르면 새로운 장면에 숫자 "3", "2", "1"이 표시된 다음 "GO!"라는 숫자가 나타납니다.

답변

3

:

코코아의 NSTimer를 사용하지 않도록하십시오. 대신 cocos2d의 자체 스케줄러를 사용하십시오.

그래서 어떤 효과가 있어도 cocos2d의 스케줄러를 사용하여 레이블에 애니메이션을 적용하는 예입니다. @ 인터페이스에서

:

초기화에서
int timeToPlay; 
CCLabelTTF * prepareLabel; 
CCLabelTTF * timeoutLabel; 
CCMenu *menu; 

:

timeToPlay=4; 

CGSize s = [CCDirector sharedDirector].winSize; 
prepareLabel = [CCLabelTTF labelWithString:@"Prepare to play!" fontName:@"Marker Felt" fontSize:40]; 
prepareLabel.position = ccp(s.width/2.0f, 150); 

timeoutLabel = [CCLabelTTF labelWithString:@"3" fontName:@"Marker Felt" fontSize:60]; 
timeoutLabel.position = ccp(s.width/2.0f, 90); 

[self addChild:prepareLabel]; 
[self addChild:timeoutLabel]; 

timeoutLabel.visible=NO; 
prepareLabel.visible=NO; 

... 
CCMenuItem *Play = [CCMenuItemFont itemFromString:@"PLAY" 
              target:self 
             selector:@selector(aboutToPlay:)]; 
... 

aboutToPlay :

-(void) aboutToPlay: (id) sender { 
    [self removeChild:menu cleanup:YES]; 
    timeoutLabel.visible=YES; 
    prepareLabel.visible=YES; 
    [self schedule: @selector(tick:) interval:1]; 
} 

그리고 체크 :

-(void) tick: (ccTime) dt 
{ 
    if(timeToPlay==1) [self play]; 
    else { 
     timeToPlay--; 
     NSString * countStr; 

     if(timeToPlay==1) 
     countStr = [NSString stringWithFormat:@"GO!"]; 
     else 
     countStr = [NSString stringWithFormat:@"%d", timeToPlay-1]; 

     timeoutLabel.string = countStr; 

     //and some cool animation effect 
     CCLabelTTF* label = [CCLabelTTF labelWithString:countStr fontName:@"Marker Felt" fontSize:60]; 

     label.position = timeoutLabel.position; 
     [self addChild: label z: 1001]; 
     id scoreAction = [CCSequence actions: 
          [CCSpawn actions: 
           [CCScaleBy actionWithDuration:0.4 scale:2.0], 
           [CCEaseIn actionWithAction:[CCFadeOut actionWithDuration:0.4] rate:2], 
           nil], 
          [CCCallBlock actionWithBlock:^{ 
           [self removeChild:label cleanup:YES]; 
          }], 
          nil]; 
     [label runAction:scoreAction]; 

    } 

} 
,536,

재생 :

-(void) play { 
    [[CCDirector sharedDirector] replaceScene:[CCTransitionSlideInL transitionWithDuration:0.4 scene:[GamePlay node]]]; 
} 
+0

당신의 상세한 답변과 내 질문에 대한 직접적인 대답을 주셔서 감사합니다. 그것은 완벽하게 작동합니다! – Zaki

+0

'init()'메소드의 처음에'if (self = [super init])'블록을 추가하는 것을 잊지 마십시오. 건배 – hyd00

2

cocos2d를 사용해야하는 경우 꼭 그렇게하십시오. 을 사용하지 않고을 사용하는 것이 더 쉽습니다. , IB에서 필요한 콘센트가있는 UILabel의를 설정 한 NSTimer 개체로 countdownTimer를 선언 한 다음의 viewDidLoad 또는 다른 곳에서 중요한 : 다음

 countdownTimer = [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(updateTime) userInfo:nil repeats:YES]; 
    label.text = @"3"; 
    [countdownTimer fire]; 

및 다음 업데이트 시간 :

- (void)updateTime { 
    if ([label.text isEqualToString:@"3"]) { 
      label.text = @"2"; 
    } else if ([label.text isEqualToString:@"2"]) { 
      label.text = @"1"; 
    } else { 
      label.text = @"GO!"; 
      [countdownTimer invalidate]; 
      //continue with app 
    } 
} 

헤이븐를 ' 해당 코드의 유효성을 확인했지만 올바른 방향으로 가야합니다! "적인 Cocos2D 모범 사례"에서

+0

: 답장을 보내 주셔서 감사합니다. 나는 이것을보고 어떻게 cocos2d를 위해 그것을 활용할 수 있는지를 볼 것이다. 다시 한번 감사드립니다. – Zaki

+2

@zaki 당신이 필요한 것을 발견했다면 큰 녹색 체크가 있다는 것을 기억하십시오! : D –

+0

: 알았어. 문제는 없지만 Cocos2d와 관련된 응답이나 응답이 더 많이 나오는지 기다리고 있습니다. 감사합니다 – Zaki