2011-03-07 12 views
1

나는 cocos2d에서 카운트 다운 타이머를 만들려고 노력하고 있지만이 문제를 해결하고 싶습니다. 내 코드가이 아래에 있습니다. 아마 논리가 잘못되었지만 수정할 수 없습니다.cocos2d의 카운트 다운 타이머?

-(id) init 
{ 
// always call "super" init 
// Apple recommends to re-assign "self" with the "super" return value 
if((self=[super init])) { 


    CCSprite *background = [CCSprite spriteWithFile:@"backgame.png"]; 
    CGSize size = [[CCDirector sharedDirector] winSize]; 
    [background setPosition:ccp(size.width/2, size.height/2)]; 

    [self addChild: background]; 


    [self schedule:@selector(countDown:)];    
} 
return self; 
} 

-(void)countDown:(ccTime)delta 
{ 

CCLabel *text = [CCLabel labelWithString:@" " 
             fontName:@"BallsoOnTheRampage" fontSize:46]; 

text.position = ccp(160,455); 
text.color = ccYELLOW; 
[self addChild:text]; 

int countTime = 20; 
while (countTime != 0) { 
    countTime -= 1; 
    [text setString:[NSString stringWithFormat:@"%i", countTime]]; 
} 

} 

답변

4

귀하의 int countTime = 20;는 또한 20 일 자체 매번 선언되면, CCLabel를 업데이트 할 수있는 시스템으로 최대한 빨리 countTimer을 감소됩니다 while 루프. 실제 타이머를 수행하려는 경우 countDown:이 호출 될 때만 감소 시키길 원합니다. while 루프가 아닙니다.

이 시도 :

@interface MyScene : CCLayer 
{ 
    CCLabel *_text; 

} 

@property (nonatomic, retain) int countTime; 

@end 

@implementation MyScene 

@synthesize countTime = _countTime; 

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


    CCSprite *background = [CCSprite spriteWithFile:@"backgame.png"]; 
    CGSize size = [[CCDirector sharedDirector] winSize]; 
    [background setPosition:ccp(size.width/2, size.height/2)]; 

    [self addChild: background]; 
    _countTime = 20; 

    _text = [CCLabel labelWithString:[NSString stringWithFormat:@"%i", self.countTime] 
             fontName:@"BallsoOnTheRampage" fontSize:46]; 

    text.position = ccp(160,455); 
    text.color = ccYELLOW; 
    [self addChild:_text]; 

    [self schedule:@selector(countDown:) interval:0.5f];// 0.5second intervals 



    } 
return self; 
} 

-(void)countDown:(ccTime)delta { 

    self.countTime--; 
    [_text setString:[NSString stringWithFormat:@"%i", self.countTime]]; 
    if (self.countTime <= 0) { 

    [self unschedule:@selector(countDown:)]; 
    } 
} 

@end 
+0

Zebulon이 답장을 보내 주신 데 대해 감사 드리며 그 코드와 설명이 오늘 당일 원했고 도움을 많이 주신 방식대로 작동하고 있습니다. 지금 많은 것을 배웠습니다. – gangmobile

+0

@gangmobile, 허용 된대로 내 대답을 확인할 수 있음을 잊지 마십시오. : D –

-1

귀하의 카운트에 allways 당신의 카운트 다운 (20)가된다.

당신은 또한 당신의 init이 이동해야합니다

CCLabel *text = [CCLabel labelWithString:@" " 
            fontName:@"BallsoOnTheRampage" fontSize:46]; 

text.position = CCP (160455); text.color = ccYELLOW; [self addChild : text];

그럼 당신은 사용해야

[self schedule:@selector(countDown:) interval:1.0f]; 

을 그리고 대신 당신이 CCLabelBMFont를 사용해야합니다 CCLabel을 사용. 훨씬 빠릅니다 :)

+0

오, 스티븐 들어 :) – Mikael

+0

이 일정을 비활성화하는 방법? – Chintan