2011-03-07 35 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

ゼブロンは返信のおかげで、スティーブンはそのコードとその日の今日の勝利は、あなたが多くのおかげで助けを期待する方法を働いて、私は今多くを学んだ。 – gangmobile

+0

@gangmobile、受け入れられたとして私の答えをチェックすることができます忘れないでください。 :D –

-1

あなたのカウントはオールウェイズあなたのカウントダウンで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

関連する問題