title: How to spawn CCSprites in random heights like flappy bird
body: <p>In the iOS game flappy bird, there are pipes that generate after a certain distance and they generate at random heights</p>
  
  <p><img src="https://i.stack.imgur.com/HJxvQ.jpg" alt="This is a fake image, but the logic is the same"></p>
  
  <p>I am also trying to make flappy bird pipes (I called it a tree branch in my code instead of pipe). Except the pipes are moving vertically instead of horizontally because it is a vertical scrolling game (it scrolls like the game <a href="https://itunes.apple.com/us/app/doodle-jump/id307727765?mt=8" rel="nofollow noreferrer">doodle jump</a>)</p>
  
  <p><strong>This is a drawing of what I want it to be: <a href="https://docs.google.com/drawings/d/18bxsVsNOlScCvgi1mwuzD2At7R6xKM3QCh6BfAVMuMo/edit?usp=sharing" rel="nofollow noreferrer">https://docs.google.com/drawings/d/18bxsVsNOlScCvgi1mwuzD2At7R6xKM3QCh6BfAVMuMo/edit?usp=sharing</a></strong>
  (The horizontal lines are the branches)</p>
  
  <p>So this is what I have tried to do so far to make the vertical branches (or pipes)...</p>
  
  <p>in my <code>.h</code></p>
  
  <pre><code>CCSprite *branch;
  NSMutableArray *_branches;
  CCSprite *obstacle;
  CCNode *previousBranch;
  CGFloat previousBranchYPosition;
  </code></pre>
  
  <p>in my <code>.m</code></p>
  
  <pre><code>@implementation HelloWorldLayer 
  
  static const CGFloat firstBranchPosition = 426.f;
  static const CGFloat distanceBetweenBranches = 140.f;
  #define ARC4RANDOM_MAX      0x100000000
  static const CGFloat minimumXPositionRightBranch = 280.f;
  static const CGFloat maximumXPositionLeftBranch = 50.f;
  static const CGFloat pipeDistance = 100.f;
  static const CGFloat maximumXPositionRightBranch = maximumXPositionLeftBranch - pipeDistance;
  </code></pre>
  
  <p><code>setBranchInitialPosition</code> method</p>
  
  <pre><code>    /* This is where I am setting the initial position of the branches. 
  So I am specifying the position of the first branch and the other branches after it so it gets placed every time a certain distance is passed. I have a left branch and a right branch*/
       -(void) setBranchInitialPosition {
          CGFloat random = ((double)arc4random() / ARC4RANDOM_MAX);
              CGFloat range = maximumXPositionRightBranch - minimumXPositionRightBranch;
  
              _rightBranch.position = ccp(minimumXPositionRightBranch + (random * range), _rightBranch.position.y);
              _leftBranch.position = ccp(_rightBranch.position.x + pipeDistance, _leftBranch.position.y);
          }
  </code></pre>
  
  <p><code>spawnNewBranches</code> method</p>
  
  <pre><code>// This is how I want the branches to spawn and I want to add them to an array full of branches
     - (void)spawnNewBranches {
          previousBranch = [_branches lastObject];
          previousBranchYPosition = previousBranch.position.y;
  
          if (!previousBranch) {
              // this is the first obstacle
              previousBranchYPosition = firstBranchPosition;
          }
  
          _rightBranch = [CCSprite spriteWithFile:@"branch.png"];
          _leftBranch = [CCSprite spriteWithFile:@"branch.png"];
          [_leftBranch addChild:_rightBranch];
          [self setBranchInitialPosition];
  
          obstacle = [CCSprite node];
          [obstacle addChild:_leftBranch];
  
  
          obstacle.position = ccp(160, previousBranchYPosition + distanceBetweenBranches);
          [self addChild:obstacle];
          [_branches addObject:obstacle];
      }
  </code></pre>
  
  <p><code>scroll</code> method</p>
  
  <pre><code>-(void) scroll:(ccTime)dt
  {
      // moves the bg
      background.position = ccp(screenCenter.x, background.position.y + [[NSUserDefaults standardUserDefaults] integerForKey:@"scrollSpeed"]*dt);
      bg2.position = ccp(screenCenter.x, background.position.y-background.contentSize.height);
  
      // it adds the new bg's to the screen before the old bg's move off the screen
      if (background.position.y &gt;= screenSize.height*1.5)
      {
          background.position = ccp(screenCenter.x, (screenCenter.y)-(background.size.height/2));
      } else if (bg2.position.y &gt;= screenSize.height*1.5) {
          bg2.position = ccp(screenCenter.x, (screenCenter.y)-(bg2.size.height/2));
      }
  
          // This is where I want them to appear every certain distance and also move with the brackground
      obstacle.position = ccp(obstacle.position.x, obstacle.position.y*[[NSUserDefaults standardUserDefaults] integerForKey:@"scrollSpeed"]*dt);
  
      NSMutableArray *offScreenObstacles = nil;
      if (obstacle.position.y &gt;= screenSize.height*1.5) {
          [offScreenObstacles addObject:obstacle];
      }
  
      for (CCNode *obstacleToRemove in offScreenObstacles) {
          [obstacleToRemove removeFromParent];
          [_branches removeObject:obstacleToRemove];
          // for each removed obstacle, add a new one
          [self spawnNewBranches];
      }
  
  }
  </code></pre>
  
  <p>Right now, the branches are appearing, but they stay in the bottom left corner and they dont move or spawn at all. I want to make them move with the background and spawn after a certain distance while also being generated in random heights. I provided you with all my code, do you know how I can make this work? Thanks in advance!</p>