Getting makeObjectsPerformSelector to Work in Objective C
I am trying to use makeObjectsPerformSelector instead of using a loop and I can't quite get it to work. I have like 20 balls that I'm trying to add to the screen inside "init".
I can add a single one by making a sprite like this:
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];
This seems to work great but 开发者_开发问答now I need to add like 20 of them. So I've decided to use an array called "balls" and I've read that I can use makeObjectsPerformSelector to perform an action on each each object in the array, but I can't figure out how to add each ball, so far I have this:
- (void) makeObjectsPerformSelector:(SEL)aSelector
{
// add the ball to the screen
[self addChild:aSelector];
}
I think that I need to use the aSelector object on the balls array and add that right? Am I using this incorrectly?
Thank you!
Based on your example, it's rather hard to tell what you're trying to do. But if you have an NSArray
called balls
, and an operation you want to perform on each ball called bounce
, you can use -makeObjectsPerformSelector:
:
[balls makeObjectsPerformSelector:@selector(bounce)];
Was that helpful at all?
A selector is not an object. It is a key which is used to look up an implementation in the method table. You should never need to override -makeObjectsPerformSelector:
精彩评论