Creating composite bodies

We also need to create a basket that can carry gifts and ornaments. Let's take a look at how to create a composite body so that we can open the top of the box and create a side and a bottom for the basket.

A composite body is a collection's physicsBody that is clubbed together to create a new body.

Getting started

Import basket.png for this section. This is all you will need.

How to do it…

Until now, we have used standard body shapes, such as circles and rectangles. To create a composite body, we can create individual shapes and attach them to the body. Let's take a look at how it is done.

Create a new function called addBasket and add the following code into it. Also, create a global CCSprite variable called basketSprite as we will need to access it later in other functions. Run the following code:

-(void)spawnBasket{

  basketSprite = [CCSprite spriteWithImageNamed:@"basket.png"];


  CCPhysicsShape *leftWall = [CCPhysicsShape
    rectShape:CGRectMake(0,0,
    10,basketSprite.contentSize.height)
    cornerRadius:0];

  CCPhysicsShape *rightWall = [CCPhysicsShape 
    rectShape:CGRectMake(basketSprite.contentSize.width - 10,0,
    10,basketSprite.contentSize.height)
     cornerRadius:0];

  CCPhysicsShape *bottom = [CCPhysicsShape 
    rectShape:CGRectMake(14,0,
    basketSprite.contentSize.width - 28,10)
    cornerRadius:0];



  NSArray* shapeArray = [[NSArray 
    alloc]initWithObjects:leftWall,
    rightWall,
    bottom,
    nil] ;


  CCPhysicsBody* basketBody = [CCPhysicsBody 
    bodyWithShapes:shapeArray];

  basketBody.type = CCPhysicsBodyTypeDynamic;

  basketSprite.physicsBody = basketBody;
  basketSprite.position = ccp(winSize.width * 0.5f, basketSprite.contentSize.height/2 + 10);

  [_physicsWorld addChild:basketSprite];

}

Here, while creating the body, we will define three CCPhysicsShapes. All three are defined as rectangles.

The first one is for the left-hand side wall of the basket, which starts in the lower-left side of the basket with a width of 10 and a height equal to the height of the basket.

The second shape starts at 10 minus the lower-right edge of the basket. Then, again, it is equal to the same width and height as the left-hand side wall.

The bottom, it starts at 14 units from the left edge with a width of 28 minus the total width of the basket. It is given a height of 10 units.

All the three shapes are added to an array, so we will create a new NSArray called shapeArray and add all the three shapes in it.

Next, we will create the physics body. While creating it, we will use the bodyWithShapes functions and pass in the array to it.

We will then set the body type to dynamic at the center of the screen but at a height of 10 from the bottom of the screen and add it to the physicsWorld node.

How it works…

Change the location of the spawn for the ornament and gift to the following so that they are a bit closer to the middle of the screen:

        [self spawnGift:CGPointMake(winSize.width/2 - winSize.width/16 , winSize.height * 0.9)];

        [self spawnOrnament:CGPointMake(winSize.width/2 + winSize.width/16 , winSize.height * 0.9)];

        [self spawnBasket];

Add the spawn basket function to the init function and run the scene with the physics body created for the basket.

How it works…
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.119.163.238