移动到NSArray中的每个位置后执行CCAction

时间:2013-02-16 21:51:45

标签: objective-c cocos2d-iphone

现在我有一个包含瓷砖坐标的NSArray,希望精灵可以遵循。 Sprite将“跳转”到每个坐标并执行CCAction,然后移动到阵列中的下一个坐标。我不确定如何处理这个问题。有什么想法吗?

1 个答案:

答案 0 :(得分:1)

使用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];

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

相关问题