Cocos2d中的动作序列

时间:2013-03-20 23:55:56

标签: cocos2d-iphone ccsprite ccaction

我有一系列精灵。我想循环遍历数组并告诉每个spirite以随机角度移动一定距离,并在这样做时淡出。最好是宽松。有没有办法设置序列来做到这一点?你有一个可变的序列,你可以在循环通过精灵数组时添加动作,然后在完成添加所有动作后运行序列吗?

1 个答案:

答案 0 :(得分:2)

我不太了解你的问题,但也许这个例子可以帮到你:

CCArray *spritesArray; //array with sprites

float timeToMove = 1.0;
float timeToRotate = 1.0;
float timeToFadeOut = 1.0;
CGPoint initialMovePos = CGPointMake(100, 100);

for (int i = 0; i<[spritesArray count]; i++) {
    id moveDistance;
    if (i == 0) {
        moveDistance = [CCMoveTo actionWithDuration:timeToMove position:initialMovePos];
    }else{
        CGPoint lastSpritePos = ((CCSprite *)[spritesArray objectAtIndex:(i-1)]).position;
        moveDistance = [CCMoveTo actionWithDuration:timeToMove position:ccpAdd(lastSpritePos, CGPointMake(10, 10))];
    }

    float angleToRotate = random()%360;
    id rotateAction = [CCRotateTo actionWithDuration:timeToRotate angle:angleToRotate];
    id fadeOutAction = [CCFadeOut actionWithDuration:timeToFadeOut];

    CCSprite *sprite = [spritesArray objectAtIndex:i];

    [sprite runAction:[CCSpawn actions:moveDistance,rotateAction,fadeOutAction, nil]];
}