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。これは true であり、 を実行しcontinue、条件を再評価し、whileというように永遠に続きます。

代わりにこれを試してください:

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

checkActionCountスケジュールされたセレクターからメソッドを呼び出します。たとえばupdate:、フレームごとに条件が評価されるようにします。

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

CCActionを 20.0 秒の期間で初期化します。で実行しますmySprt。これにより、numberofRunningActions カウントが 1 増加します。

これは while ループでチェックインしているものであり、1 をログに記録します。アクションが終了する 20 秒後。ログは 0 になります (他のアクションを追加しない限り)。

于 2014-03-26T12:22:23.667 に答える