【问题标题】:how to move sprite with delay in cocos2d?如何在 cocos2d 中延迟移动精灵?
【发布时间】:2014-01-16 13:42:57
【问题描述】:

我在数组中有多个精灵。现在我想用延迟时间 0.5 移动那个精灵。当时我使用下面的代码所有精灵都同时下降,但我想一个一个下降精灵。我也使用 CCDelay 方法但也没有得到所需的结果.

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循环在动作运行后完成,以便所有精灵具有相同的动作和相同的时间。

每次进入 for 循环时如何运行操作?

我也试试COCOS2D: how to animate falling bricks into a grid

【问题讨论】:

    标签: ios cocos2d-iphone


    【解决方案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。

    【讨论】:

      【解决方案2】:

      经过很长时间,通过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];
      
                                      }
                          });
      

      【讨论】:

        猜你喜欢
        • 2012-02-04
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-07-07
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多