我想让这个类通过传递不同的名字来动态地执行它自己的函数。那可能吗 ?或者说:它怎么可能?我可以传递函数名称作为参数吗?
-(id)initWithMethod:(NSString*)method{
if ((self = [super init])){
[self method];
}
return self;
}
-(void) lowHealth {
CCSprite *blackScreen = [CCSprite spriteWithFile:@"blackscreen.png"];
blackScreen.anchorPoint = ccp(0,0);
[self addChild:blackScreen];
id fadeIn = [CCFadeIn actionWithDuration:1];
id fadeOut = [CCFadeOut actionWithDuration:1];
id fadeInAndOut = [CCRepeatForever actionWithAction:[CCSequence actions:fadeIn, fadeOut, nil]];
[blackScreen runAction:fadeInAndOut];
}
你可以用块变量或通过编写代码内嵌调用此我认为,不应该使用NSSelectorFromString(),而应该在'-initWithSelector中传递一个选择器:(SEL)aSector' – vikingosegundo 2011-12-26 23:18:38
在调用performSelector之前调用respondsToSelector也是可取的,既可以作为断言,也可以在一般情况下避免崩溃,如果实例不不支持那个选择器。 – LearnCocos2D 2011-12-29 20:37:17