title: SpriteKit collision detection on multiple objects of the same type
body: <p>I am currently working on a game with a friend right now using Objective-C and SpriteKit. My friend and I are both new to SpriteKit, so this project we are working on is more of just a learning experience. But of course, we are running in to some trouble.</p>
  
  <p>The game is fairly simple: There is a ball that fly's around the screen and bounces off of the "walls", or edges of the iPhone screen. The user controls a paddle at the bottom of the screen to deflect the ball back up to prevent the ball from reaching the bottom of the screen. Each time the ball hits the paddle, the score counter at the top of the screen increments by one.</p>
  
  <p>Here is what we are struggling to accomplish. When the score of the game is equal to 5, we add a new ball to the scene. That new ball begins to move by itself, but whenever we try to detect if that new ball has hit an edge of the screen, the collision is not being detected.</p>
  
  <p>Here is our method which returns a ball object:</p>
  
  <pre><code>+(id)ball
  {
      // the ball is a random image from google
      Ball *ball = [Ball spriteNodeWithImageNamed:@"ball"];
  
      // set the position of the ball
      ball.position = CGPointMake(0, 80);
  
      // set ball name property
      ball.name = @"ball";
  
      // give the ball a physics body
      ball.physicsBody = [SKPhysicsBody bodyWithRectangleOfSize:ball.size];
  
      ball.physicsBody.affectedByGravity = NO;
  
      return ball;
  }
  </code></pre>
  
  <p>This method is called to move the balls in the scene:</p>
  
  <pre><code>-(void)move:(int)deltaX withDeltaY:(int)deltaY
  {
      SKAction *testMoveRight = [SKAction moveByX:deltaX y:deltaY duration:0.03];
  
      // this will repeat the action over and over
      SKAction *move = [SKAction repeatActionForever:testMoveRight];
      [self runAction:move];
  }
  </code></pre>
  
  <p>These are our categories for collision detection:</p>
  
  <pre><code>// this category is for the original ball in the game
  static const uint32_t ballCategory = 0x1 &lt;&lt; 0;
  // this category is for the second ball that gets added to the game
  static const uint32_t ball2Category = 0x1 &lt;&lt; 1;  
  static const uint32_t paddleCategory = 0x1 &lt;&lt; 2;
  static const uint32_t topBarrierCategory = 0x1 &lt;&lt; 3;
  static const uint32_t leftBarrierCategory = 0x1 &lt;&lt; 4;
  static const uint32_t rightBarrierCategory = 0x1 &lt;&lt; 5;
  static const uint32_t gameOverBarrierCategory = 0x1 &lt;&lt; 6;
  </code></pre>
  
  <p>And here is our didBeginContact method:</p>
  
  <pre><code>-(void)didBeginContact:(SKPhysicsContact *)contact
  {
      SKPhysicsBody *firstBody, *secondBody;
  
      if (contact.bodyA.categoryBitMask &lt; contact.bodyB.categoryBitMask) {
          firstBody = contact.bodyA;
          secondBody = contact.bodyB;
      }
      else {
          firstBody = contact.bodyB;
          secondBody = contact.bodyA;
      }
  
  
      // first ball contact
      if ((firstBody.categoryBitMask &amp; ballCategory) != 0 &amp;&amp; (secondBody.categoryBitMask &amp; rightBarrierCategory) != 0) {
          [ball move:-15 withDeltaY:0];
      }
  
      else if ((firstBody.categoryBitMask &amp; ballCategory) != 0 &amp;&amp; (secondBody.categoryBitMask &amp; topBarrierCategory) != 0) {
          [ball move:0 withDeltaY:-20];
      }
  
      else if ((firstBody.categoryBitMask &amp; ballCategory) != 0 &amp;&amp; (secondBody.categoryBitMask &amp; leftBarrierCategory) != 0) {
          [ball move:15 withDeltaY:0];
      }
  
      else if ((firstBody.categoryBitMask &amp; ballCategory) != 0 &amp;&amp; (secondBody.categoryBitMask &amp; paddleCategory) != 0) {
          [ball move:0 withDeltaY:20];
  
          // increment score
          self.score++; 
          // update the score label
          self.deathLabel.text = [NSString stringWithFormat:@"%i", self.score];
  
          // add a new ball to the scene if the score is 5
          if (score == 5) {
              [scene addChild:ball2];
              [ball2 move:5 withDeltaY:10];
          }
      }
  
      // second ball contact detection
      if ((firstBody.categoryBitMask &amp; ball2Category) != 0 &amp;&amp; (secondBody.categoryBitMask &amp; rightBarrierCategory) != 0) {
          [ball2 move:-15 withDeltaY:0];
      }
  }
  </code></pre>
  
  <p>Why doesn't that second ball we add to the scene detect when it is colliding with the right barrier (right edge of the screen)? When it reaches the right edge of the screen, it doesn't bounce off and move left like we want it to. Any suggestions? </p>