gpt4 book ai didi

iphone - 倒数计数器/定时器

转载 作者:行者123 更新时间:2023-12-03 20:31:42 28 4
gpt4 key购买 nike

请谁能告诉我如何在 iPhone 的 cocos2d 中实现启动游戏的倒计时器。

我的意思是,按下“播放”时,一个新场景会出现,显示数字“3”、“2”、“1”,然后显示“GO!”一词。

最佳答案

来自“cocos2d最佳实践”:

Try NOT to use Cocoa’s NSTimer. Instead use cocos2d’s own scheduler.

这是使用 cocos2d 的调度程序为标签设置动画的示例,即使有一些效果。

在@interface中:

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:)];
...

关于播放:

-(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];

}

}

播放:

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

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

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