Search code examples
iosobjective-csprite-kitskphysicsbodyskphysicsworld

bodyWithBodies SKPhysicsBody


Apple's documentation is vague when it comes to explaining how to effectively implement this class method

+ (SKPhysicsBody *)bodyWithBodies:(NSArray *)bodies; //the error that I'm getting is 2014-04-23 23:25:21.518 Space Monkey[3398:60b] -[SKSpriteNode _shapes]: unrecognized selector sent to instance 0x15d79c50

2014-04-23 23:25:21.520 Space Monkey[3398:60b] * Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[SKSpriteNode _shapes]: unrecognized selector sent to instance 0x15d79c50' * First throw call stack: (0x2d92ef03 0x380c3ce7 0x2d932837 0x2d93112f... It is throwing an exception and I've pretty much researched everywhere with no luck. There is another question about the same issue on here however the suggestions don't seem to work at all. I've provided the code in order for someone to shed some light and hopefully direct me in the correct...Here's the code

-(NSMutableArray *)platformBars
 {
   NSMutableArray *array =[[NSMutableArray alloc]init];
   //_spriteNode declared in the interface as an instance variable
   _spriteNode=[SKSpriteNode node];
   for (int i =0; i<5; i++)
      {
        _spriteNode= [SKSpriteNode spriteNodeWithImageNamed:@"Water Block"];
        _spriteNode.physicsBody.mass = 10.0f;
        _spriteNode.physicsBody.density = _spriteNode.size.height;
        _spriteNode.position = CGPointMake(x , _spriteNode.position.y);
        x=x+40;
        [array addObject:_spriteNode];
      }
      return array;
  }

-(PlatformNode *)createPlatformsAtPosition:(CGPoint)position
 {
   NSArray *arrays = [self platformBars];
   PlatformNode *node =[PlatformNode node];
   node.physicsBody = [SKPhysicsBody bodyWithBodies:arrays];
   for (SKSpriteNode *sknode in arrays)
       {
         [node addChild:sknode];
       }
   node.position=position;
   node.name    =@"platform";
   node.physicsBody.affectedByGravity  = NO;
   NSLog(@"bodyofSize %f",[node calculateAccumulatedFrame].size.width);
   return node;
  }

  -(PlatformNode *)createPlatformsAtPosition:(CGPoint)position is called in the  
  -(void)didMoveToView:(SKView *)view body of the function. 

I hope i included enough details and a snippet of my code so hopefully will someone will help me and others to come as well. Thank you in advance.


Solution

  • Should be

    NSArray *arrays = [[self platformBars] copy];
    

    instead of

    NSArray *arrays = [self platformBars];
    

    Because [self platformBars] is mutable array and arrays is immutable.
    Edit
    And, btw in arrays are SKSpriteNodes, not physics bodies. So

    node.physicsBody = [SKPhysicsBody bodyWithBodies:arrays];
    

    should be

    NSMutableArray *mArray=[NSMutableArray new];
    for (SKSpriteNode *anySpriteNode in arrays){
    [mArray addObject:anySpriteNode.physicsBody];
    }
    node.physicsBody = [SKPhysicsBody bodyWithBodies:mArray.copy];