0

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

4

1 回答 1

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]];
}
于 2013-03-21T04:41:52.643 回答