Cocos2D定义了Sequence类的静态方法'actions':
+(id) actions: (FiniteTimeAction *) action1, ... { /* omitted */ }
如何构建要在运行时执行的操作列表,可能是从磁盘文件中读取的?
我读到可变长度参数列表可以转换为(char *)并以这种方式传递...
NSMutableArray *actions = [[NSMutableArray alloc] init];
[actions addObject: [DelayTime actionWithDuration:1]];
[actions addObject: [ScaleBy actionWithDuration:2 scale:4];
char *argList = (char *)malloc(sizeof(FiniteTimeAction *) * [actions count]);
[actions getObjects:(id *)argList];
[self runActions: actions];
这是“最佳方式”还是“正确”方式?他们是更好的替代品,更快的替代品吗?
答案 0 :(得分:2)
vaargs只是构建嵌套Sequence
对象的助手。它返回一个FiniteTimeAction*
,它是通过对[Sequence actionOne:one_ two:two_]
的连续调用构建的。您可以通过循环遍历您的集合或数组来自己在代码中执行此操作。它应该是这样的:
FiniteTimeAction *seq = nil;
for (FiniteTimeAction *action in actions) {
if (!seq) {
seq = action;
} else {
seq = [Sequence actionOne:seq two:action];
}
}
[self runActions:seq];
答案 1 :(得分:0)
我会考虑使用NSInvocation - 你基本上可以使用你所针对的方法签名来构建一个,然后像这样使用每个对象的setter:
NSInvocation *invoker = setup invoker here...
for ( int i = 0; i < actions.count; i++ )
{
NSObject *arg = [actions objectatIndex:i];
[invoker setArgument:&arg atIndex:i+2];
}
[invoker setArgument:nil atIndex:i+2];
i + 2位是因为前两个参数实际上是self和_cmd,因此您可以设置索引2和...的所有内容...在NSInvocation中读取setArgument:atIndex:上的文档以获取更多详细信息。
完成后,您可以使用目标调用操作。我从来没有在变量参数方法中使用它,所以我不确定它在那里工作得有多好,但它是我知道在运行时动态构造一个调用的唯一方法。
答案 2 :(得分:0)
鉴于提供的选项,似乎唯一可行的方法是使用我在问题中提到的方法,即:
NSMutableArray *actions = [[NSMutableArray alloc] init];
[actions addObject: [DelayTime actionWithDuration:1]];
[actions addObject: [ScaleBy actionWithDuration:2 scale:4];
char *argList = (char *)malloc(sizeof(FiniteTimeAction *) * [actions count]);
[actions getObjects:(id *)argList];
[self runActions: actions];
答案 3 :(得分:-1)
也许是一组预建的序列?
id move = [MoveBy actionWithDuration:3 position:ccp(350,0)];
id move_back = [move reverse];
id move_ease_in = [EaseIn actionWithAction:[[move copy] autorelease] rate:3.0f];
id move_ease_in_back = [move_ease_in reverse];
id move_ease_out = [EaseOut actionWithAction:[[move copy] autorelease] rate:3.0f];
id move_ease_out_back = [move_ease_out reverse];
id seq1 = [Sequence actions: move, move_back, nil];
id seq2 = [Sequence actions: move_ease_in, move_ease_in_back, nil];
id seq3 = [Sequence actions: move_ease_out, move_ease_out_back, nil];
[grossini runAction: [RepeatForever actionWithAction:seq1]];
[tamara runAction: [RepeatForever actionWithAction:seq2]];
[kathia runAction: [RepeatForever actionWithAction:seq3]];