Objective c 让makeObjectsPerformSelector在目标C中工作

Objective c 让makeObjectsPerformSelector在目标C中工作,objective-c,xcode,ipad,cocos2d-iphone,Objective C,Xcode,Ipad,Cocos2d Iphone,我试图使用makeObjectsPerformSelector而不是使用循环,但我无法让它正常工作。我有20个球,我正试图添加到“init”内的屏幕上 我可以通过制作这样一个精灵来添加一个: CCSprite *ball = [CCSprite spriteWithFile:@"ball.png" rect:CGRectMake(0, 0, 20, 20)]; // set the position of the ball providing the coordinates

我试图使用makeObjectsPerformSelector而不是使用循环,但我无法让它正常工作。我有20个球,我正试图添加到“init”内的屏幕上

我可以通过制作这样一个精灵来添加一个:

CCSprite *ball = [CCSprite spriteWithFile:@"ball.png" rect:CGRectMake(0, 0, 20, 20)];

        // set the position of the ball providing the coordinates
        ball.position = ccp((player.contentSize.width/2 + 400)+ball.contentSize.width/2, winSize.height/2 - ball.contentSize.height/2);

        // add the ball to the playing area
        [self addChild:ball];
这似乎很有效,但现在我需要添加20个。所以我决定使用一个名为“balls”的数组,我读到我可以使用makeObjectsPerformSelector对数组中的每个对象执行操作,但我不知道如何添加每个球,到目前为止我有:

- (void) makeObjectsPerformSelector:(SEL)aSelector
{

    // add the ball to the screen
    [self addChild:aSelector];

}
我想我需要在balls数组中使用aSelector对象并添加它,对吗?我用错了吗


谢谢大家!

选择器不是对象。它是一个键,用于在方法表中查找实现。您不应该需要重写
-makeObjectsPerformSelector:

根据您的示例,很难判断您要做什么。但是,如果您有一个名为
balls
NSArray
,并且希望对每个球执行一个名为
bounce
的操作,则可以使用
-makeObjectsPerformSelector:

[balls makeObjectsPerformSelector:@selector(bounce)];

这有帮助吗?

好的,对不起,我想我用错了方法。为了在“balls”数组中的每个对象上执行特定的函数,使用for循环的唯一方法是什么?循环每一个并添加它?感谢您对(数组中的id对象){//do something with object}的响应。如果您希望对每个球执行的操作是球的类上的方法,那么您可以使用
-makeObjectsPerformSelector:
,如我所示。但是,如果需要对球执行更复杂的操作(例如,球是消息的参数或类似于
[self bounce:ball]
),则需要对
循环使用
。感谢您的回复NSResponder,这很有帮助