2014-01-16 44 views
-3

我有一些精靈在數組中。現在我想用延遲時間0.5來移動該精靈。當時我在下面的代碼中使用所有精靈都在同一時間下降,但我想逐個降低精靈。我也使用CCDelay方法,但也沒有得到所需的結果。如何在cocos2d中延遲移動精靈?

for (int j = 1; j < [ary count]; j++) 
{ 
    torpedoOne.position = ccp(160,580); 

    id actionMove = [CCMoveTo actionWithDuration:2.0 
             position:ccp(30 + (j*25),300)]; 

    id deleay = [CCDelayTime actionWithDuration:1.0]; 


    [torpedoOne runAction:[CCSequence actions:actionMove,deleay,nil]]; 

    [self addChild:torpedoOne]; 

} 

首先for循環完成後,行動運行,使所有精靈具有相同的時間acion。

如何在每次進入循環時運行動作?

我是ASLO嘗試COCOS2D: how to animate falling bricks into a grid

回答

0

經過很長時間,我用cocos2d中的特定延遲與動畫一起逐個獲取動畫。

dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_LOW, 0), 
         ^{ 
           for (int j = 1; j < [ary count]; j++) 
           { 
            dispatch_async(dispatch_get_main_queue(), 
            ^{ 

            torpedoOne.position = ccp(160,580); 

            id actionMove = [CCMoveTo actionWithDuration:2.0 
              position:ccp(30 + (j*25),300)]; 

            id deleay = [CCDelayTime actionWithDuration:1.0]; 


            [torpedoOne runAction:[CCSequence actions:actionMove,deleay,nil]]; 

            [self addChild:torpedoOne]; 

            }); 

           [NSThread sleepForTimeInterval:delay]; 

           } 
        }); 
1

你的邏輯很奇怪。嘗試

for (int j = 0;j<[ary count]; j++{  // gets all objects in ary : 0 to count-1 
    torpedoOne = [ary objectAtIndex:j]; // I am assuming this is what you wanted 

    torpedoOne.position = ccp(160,580); 
    id actionMove = [CCMoveTo actionWithDuration:2.0 
             position:ccp(30 + (j*25),300)]; 
    float delayTime = j*0.5f; 
    torpedoOne.visible = NO; 
    id show = [CCShow action]; // if you want them invisible prior to start move 
    id delay = [CCDelayTime actionWithDuration:delayTime]; 
    [torpedoOne runAction:[CCSequence actions:delay,show,actionMove,nil]]; 
} 

此外,你應該設置torpedoOne內循環。