Iphone cocos2d中的三秒倒计时
我试图有一个3秒倒计时显示时,一个水平是在我的游戏击败。这就是我所做的:Iphone cocos2d中的三秒倒计时,iphone,ios,objective-c,cocos2d-iphone,Iphone,Ios,Objective C,Cocos2d Iphone,我试图有一个3秒倒计时显示时,一个水平是在我的游戏击败。这就是我所做的: -(void) gameSegmentBeat { [self pauseSchedulerAndActions]; // Overlay the game with an opaque background CCSprite *opaqueBG = [CCSprite spriteWithFile:@"background1.png"]; opaqueBG.position = scre
-(void) gameSegmentBeat {
[self pauseSchedulerAndActions];
// Overlay the game with an opaque background
CCSprite *opaqueBG = [CCSprite spriteWithFile:@"background1.png"];
opaqueBG.position = screenCenter;
[self addChild:opaqueBG z:10000];
// These are the 3 labels for the 3 seconds
CCLabelTTF *three = [CCLabelTTF labelWithString:@"3" fontName:@"Helvetica" fontSize:100];
three.position = ccp(screenCenter.x, screenCenter.y);
CCLabelTTF *two = [CCLabelTTF labelWithString:@"2" fontName:@"Helvetica" fontSize:100];
two.position = ccp(screenCenter.x, screenCenter.y);
CCLabelTTF *one = [CCLabelTTF labelWithString:@"1" fontName:@"Helvetica" fontSize:100];
one.position = ccp(screenCenter.x, screenCenter.y);
// Secondspast is specified in the update method
secondspast = 0;
if (secondspast == 1) {
[self addChild:three z:10001];
} else if (secondspast == 2) {
[self removeChild:three];
[self addChild:two z:10001];
} else if (secondspast == 3) {
[self removeChild:two];
[self addChild:one z:10001];
}
}
以及更新:
framespast = 0;
-(void)update:(ccTime)delta {
framespast++;
secondspast = framespast / 60;
}
我调用了gameSegmentBeat
,这是我在上面没有展示的方法之一。。。我知道人们之所以叫它是因为:1。pauseSchedulerAndActions
正在工作,2。此时将显示CCSprite*opaqueBG
另外,我应该补充的是,如果我移动[self addChild:three z:10001]代码>在它当前所在的if语句之外,然后标签会显示出来,但一旦我移回if语句,它就不起作用了…
我不知道为什么,但是标签并不是每秒钟都在更换,所以没有倒计时
提前谢谢 您的标签未被切换,因为切换标签的代码未被调用(至少,它在您共享的代码中不可见) 从更新循环中调用
gameSegmentBeat
-(void)update:(ccTime)delta {
framespast++;
secondspast = framespast / 60;
[self gameSegmentBeat];
}
这个
将阻止调用更新方法。正如它所说,这会暂停调度程序
若要测试是否调用了update。您可以使用定时执行块来延迟1、2和3秒执行:
dispatch_time_t countdownTime1 = dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1 * NSEC_PER_SEC));
dispatch_after(countdownTime1, dispatch_get_main_queue(), ^(void){
[self addChild:three z:10001];
});
dispatch_time_t countdownTime2 = dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2 * NSEC_PER_SEC));
dispatch_after(countdownTime2, dispatch_get_main_queue(), ^(void){
[self removeChild:three];
[self addChild:two z:10001];
});
dispatch_time_t countdownTime3 = dispatch_time(DISPATCH_TIME_NOW, (int64_t)(3 * NSEC_PER_SEC));
dispatch_after(countdownTime3, dispatch_get_main_queue(), ^(void){
[self removeChild:two];
[self addChild:one z:10001];
});
没什么
gameSegmentBeat
被调用一次,其中secondspast的值被设置为0,因此如果满足条件,则没有,因此您看不到任何标签
gamesectionbeat
调用pauseSchedulerAndActions
,该函数暂停所有计划的选择器和操作,因此不会调用函数update
,因此SecondsPost的值不会更改。即使它确实改变了,在上面的代码中也不重要,因为更新中没有调用GameSectionBeat-(void) visit{
framespast++;
secondspast = framespast / 60;
if (secondspast == 1) {
[self addChild:three z:10001];
} else if (secondspast == 2) {
[self removeChild:three];
[self addChild:two z:10001];
} else if (secondspast == 3) {
[self removeChild:two];
[self addChild:one z:10001];
}
[super visit];
}
Gamebeat在哪里?我猜它被调用过一次。我更新了问题。是的,我调用了标签开关,当我游戏中的一个对象碰到另一个对象时。我更新了这个问题。你知道我可以用另一种方法切换标签(或倒计时)吗,这种方法可以与
暂停日程安排操作
?谢谢谢谢你不仅解决了我的问题,还教会了我一些新的东西+1并接受:)
-(void) visit{
framespast++;
secondspast = framespast / 60;
if (secondspast == 1) {
[self addChild:three z:10001];
} else if (secondspast == 2) {
[self removeChild:three];
[self addChild:two z:10001];
} else if (secondspast == 3) {
[self removeChild:two];
[self addChild:one z:10001];
}
[super visit];
}