我正在尝试添加几个顺序出现的标签,每个标签之间有一段时间延迟。标签将显示0或1,并且值是随机计算的。我正在运行以下代码:
for (int i = 0; i < 6; i++) {
NSString *cowryString;
int prob = arc4random()%10;
if (prob > 4) {
count++;
cowryString = @"1";
}
else {
cowryString = @"0";
}
[self runAction:[CCSequence actions:[CCDelayTime actionWithDuration:0.2] ,[CCCallFuncND actionWithTarget:self selector:@selector(cowryAppearWithString:data:) data:cowryString], nil]];
}
使标签出现的方法是:
-(void)cowryAppearWithString:(id)sender data:(NSString *)string {
CCLabelTTF *clabel = [CCLabelTTF labelWithString:string fontName:@"arial" fontSize:70];
CGSize screenSize = [[CCDirector sharedDirector] winSize];
clabel.position = ccp(200.0+([cowries count]*50),screenSize.height/2);
id fadeIn = [CCFadeIn actionWithDuration:0.5];
[clabel runAction:fadeIn];
[cowries addObject:clabel];
[self addChild:clabel];
}
此代码的问题在于所有标签在同一时刻出现并具有相同的延迟。我理解,如果我使用[CCDelayTime actionWithDuration:0.2*i]
,代码将起作用。但问题是我可能还需要迭代整个for循环并让标签在第一次出现后再次出现。怎么可能让动作出现延迟并且动作不总是遵循相同的顺序或迭代???
答案 0 :(得分:14)
也许我真的不明白你想做什么。但是,如果你需要一些控制标签出现(迭代某些东西),可以这样做:
-(void) callback
{
static int counter = 0;
//create your label and label action here
// iterate through your labels if required
counter++;
if (counter < 6)
{
double time = 0.2;
id delay = [CCDelayTime actionWithDuration: time];
id callbackAction = [CCCallFunc actionWithTarget: self selector: @selector(callback)];
id sequence = [CCSequence actions: delay, callbackAction, nil];
[self runAction: sequence];
}
else
{
//calculate the result and run callback again if required
//don't forget to write counter = 0; if you want to make a new throw
}
}
答案 1 :(得分:2)
问题在于您正在安排所有动作同时启动。
更改
[self runAction:[CCSequence actions:[CCDelayTime actionWithDuration:0.2] ,[CCCallFuncND actionWithTarget:self selector:@selector(cowryAppearWithString:data:) data:cowryString], nil]];
的
[self runAction:[CCSequence actions:[CCDelayTime actionWithDuration:0.2 * i] ,[CCCallFuncND actionWithTarget:self selector:@selector(cowryAppearWithString:data:) data:cowryString], nil]];
应解决您的问题