아이폰 용 cocos2d에서 게임을 시작하기위한 카운트 다운 타이머를 구현하는 방법을 보여 주실 수 있습니까?카운트 다운 카운터/타이머
내가 말하는 것은 "재생"을 누르면 새로운 장면에 숫자 "3", "2", "1"이 표시된 다음 "GO!"라는 숫자가 나타납니다.
아이폰 용 cocos2d에서 게임을 시작하기위한 카운트 다운 타이머를 구현하는 방법을 보여 주실 수 있습니까?카운트 다운 카운터/타이머
내가 말하는 것은 "재생"을 누르면 새로운 장면에 숫자 "3", "2", "1"이 표시된 다음 "GO!"라는 숫자가 나타납니다.
:
코코아의 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]]];
}
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 모범 사례"에서
당신의 상세한 답변과 내 질문에 대한 직접적인 대답을 주셔서 감사합니다. 그것은 완벽하게 작동합니다! – Zaki
'init()'메소드의 처음에'if (self = [super init])'블록을 추가하는 것을 잊지 마십시오. 건배 – hyd00