cocos2d中多个CCSprite循环播放CCMoveBy动画的问题


想做一个持续运动的效果。采用 NSTimer 来计时触发动画:

```objective-c [NSTimer scheduledTimerWithTimeInterval:2.0f target:self selector:@selector(melonPush) userInfo:nil repeats:YES];


 `melonPush`方法是这么写的:

```objective-c
- (void)melonPush
{
    CCSprite *walnut = [walnutSprites lastObject];

    if (walnut.position.x >= 470) {
        return;
    }

    CCMoveBy *push = [CCMoveBy actionWithDuration:.1f position:ccp(10, 0)];
    [melonSprite runAction:push];
    for (int i = [walnutSprites count] - 1; i >= 0; i--) {
        CCSprite *nut = [walnutSprites objectAtIndex:i];
        [nut runAction:push];
    }
}

但是在运行的时候,只有最后一个应用 runAction 的对象才有动画,其它的对象都不动。这是怎么回事?

动画 ios cocos2d objective-c

PPPoE 11 years, 2 months ago

找到问题了, CCMoveBy 对象要在每次应用动画之前都得创建一次,就像这样:


 for (int i = [walnutSprites count] - 1; i >= 0; i--) {
        CCMoveBy *push = [CCMoveBy actionWithDuration:.1f position:ccp(10, 0)];
        CCSprite *nut = [walnutSprites objectAtIndex:i];
        [nut runAction:push];
}

十六夜鈴仙 answered 11 years, 2 months ago

Your Answer