0

下面的示例代码永远不会出现在 while 循环中,并且它总是将正在运行的操作数打印为 1。我错过了什么?

在此先感谢克里希纳

-(id)init
{    
    if(self == [super init])
    {  
    CCSPrite *mySprt = [CCSprite spriteWithFile:@"myFile.png"];  
    mySprt.position = ccp(160,240);  
    mySprt.tag = 331; 
    CCFadeTo *fadeSprt = [CCFadeTo actionWithDuration:20.0 opacity:0];  
    [mySprt runAction:fadeSprt];  
    [self addChild:mySprt];  
    [self checkActionCount];
   }  
   return self;  

}

-(void)checkActionCount  
{  
while([self getchildByTag:331].numberofrunningactions > 0)  
    {  
     NSLog(@"Number of actions = %d",[self getchildByTag:331].numberofrunningactions);  
     continue;  
    }  
NSLog(@"Number of actions = %d",[self getchildByTag:331].numberofrunningactions);  
}
4

2 回答 2

1

你有一个无限循环:

while([self getchildByTag:331].numberofrunningactions > 0)  
{  
     NSLog(..);  
     continue;
}  

continue语句将退出当前块以重新评估while条件,这是真的,这将执行 a continue,并重新评估while条件,以此类推,直到永远。

而是试试这个:

if ([self getchildByTag:331].numberofrunningactions > 0)  
{  
     NSLog(..);  
}  

并从计划的选择器中调用checkActionCount方法,例如 instace update:,以便每帧评估一次条件。

于 2014-03-26T12:31:10.080 回答
0
CCFadeTo *fadeSprt = [CCFadeTo actionWithDuration:20.0 opacity:0];      
[mySprt runAction:fadeSprt];  

您初始化 aCCAction持续时间为 20.0 秒。现在你在mySprt. 这会将 numberofRunningActions 计数增加 1。

这就是您在 while 循环中检查的内容,它会记录 1. 20 秒后操作完成后。它将记录 0(除非您添加其他操作)。

于 2014-03-26T12:22:23.667 回答