目标c-移动到NSArray中的每个位置后,预先形成CCAction



现在我有一个NSArray,其中包含我希望精灵遵循的平铺坐标。精灵将"跳跃"到每个坐标,并在移动到阵列中的下一个坐标之前预生成一个CCAction。我不确定如何处理这个问题。有什么想法吗?

使用CCSequence,您可以为要到达的每个点生成一个操作。我的例子假设你正在包装CGPoint,这很有指示性,它可能不会做你想要的事情,但它只是给你一个想法:

// I suppose that you wrapped CGPoint with an object able to return the x and y coordinates.
// points contains all these coordinate objects.
NSMutableArray* actions= [[NSMutableArray alloc]initWithCapacity: points.count];
for(NSUInteger i=0; i<points.count; i++)
{
    id coordinate= points[i];
    CGPoint point= CGPointMake(coordinate.x, coordinate.y);
    // Change this code to whatever is needed to initialize the point.
    CCMoveTo* move= [CCMoveTo actionWithDuration: duration position: point];
    // I suggest to compute duration in a way that it depends from the speed, so
    // that the sprite moves with constant speed.
    [actions addObject: move];      
}
CCSequence* sequence= [CCSequence actionsWithArray: actions];
[sprite runAction: sequence];

我直接在浏览器中输入了它,我只是希望没有语法错误,在这种情况下请告诉我。

最新更新