gpt4 book ai didi

iphone - Cocos2d中的倒数计时器?

转载 作者:行者123 更新时间:2023-12-01 19:28:12 25 4
gpt4 key购买 nike

我正在尝试在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]];
}

}

最佳答案

您的int countTime = 20;每次都会声明为20。而且,您的while循环将以系统可以更新CCLabel的速度尽 express 减countTimer。如果您尝试执行一个真正的计时器,则只希望在调用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

关于iphone - Cocos2d中的倒数计时器?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5223320/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com