text: iphone - Acting on all sprites from the add new sprite method cocos2d xcode - Stack Overflow
  I am building a game for iphone, I am new to game devolopment and somewhat new to the language. I have balls that fall from the top of the screen to the bottom and the player is supposed to try and catch them. The problem is, I add the new sprites with an "addnewBallSprite" method, and later act on the position in my UpdateEveryFrame method. But if there is still a ball on the screen when a new ball is created the old ball stops moving. Is there a way I can make my change in position command control all instances of the ball sprite??? Please explain in detail. Below is my addNewBall method:
  int RandomBallSprite = (arc4random() % 5);
  [self addChild:Ball z:1];
  iphone xcode cocos2d-iphone sprite share|improve this question edited Jan 31 '11 at 0:08
  asked Jan 29 '11 at 4:41
  If you want to run some action (i mean CCAction or something other if you want) on the sprite you should have a pointer to this sprite. So you can create an NSMutableArray to keep those pointers and then to interact with them. If the only children of you CCLayer are balls - you can take there pointers via calling children method on your CCLayer.
  If you have many balls the only way to interact with them is to keep their pointers. Add this declaration to your class:
  NSMutableArray *ballArray_;
  Initialize array in your class init method:
  ballArray_ = [[NSMutableArray alloc] init];
  When you create a new ball add the ball to the array:
  [ballArray addObject:myNewBall];
  And now in your update method you can have the access to each ball in your scene:
  for (Ball *ball in ballArray)
  //increase the position of the ball
  When the ball is catched by User or out of the scene remove it from array:
  [ballArray removeObject: someBall];
  share|improve this answer edited Jan 30 '11 at 8:04
  answered Jan 29 '11 at 9:19
  Maybe I have taken the wrong approch, My UpdateFrame method just increments the y position vector. Ball.position = ccp(Ball.position.x, Ball.postion.y + 100*dt); should I be using a cocos2d action to move my sprites? By pointer you mean the "ball.position"???
  By pointer i mean Ball. Your approach is not bad at all. It depends on situation. You are using Ball when you are changing it's position. So if you want to change position of several balls you should have their pointers too.
  Ok, I see that that makes since, however my AddNewBall method calls all of the balls "Ball" Is there a way to change that? Thanks for all your help.
  I don't understand what exactly your method AnnNewBall dose. Provide the code please.
  I have added the method to my question, Later in another method I call on Ball.position again to increment it and make it move down the screen..
  Not the answer you're looking for?	Browse other questions tagged iphone xcode cocos2d-iphone sprite or ask your own question.	asked