Question

Below sample code never come out of while loop and its printing the number of running actions as 1 always. What I am missing?

Thanks in advance Krishna

-(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);  
}
Was it helpful?

Solution

You have an endless loop:

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

The continue statement will exit the current block to re-evaluate the while condition, which is true, which will do a continue, and re-evaluate the while condition, and so on for all eternity.

Instead try this:

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

and call the checkActionCount method from a scheduled selector, for instace update:, so that the condition is evaluated once every frame.

OTHER TIPS

CCFadeTo *fadeSprt = [CCFadeTo actionWithDuration:20.0 opacity:0];      
[mySprt runAction:fadeSprt];  

You initialize a CCAction with duration 20.0 seconds. Now you run it on the mySprt. this increase the numberofRunningActions count by 1.

which is what you are checking in while loop and it logs 1. After 20 seconds when the action finishes. it will log 0 (unless you add other action).

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top