O'Reilly logo

Cocos2d Cross-Platform Game Development Cookbook - Second Edition by Siddharth Shekar

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Adding sprite texture to physics objects

Until now, we have only added physics bodies, but we haven't added any texture to them. Let's take a look at how to do it.

Getting started

Import the gift.png and ornament.png resources to the file.

How to do it…

It looks as though it is going to be raining gifts, so we will add a new function called spawnGift and the following code:

- (void)spawnGift:(CGPoint) position{

  CCSprite *giftSprite = [CCSprite spriteWithImageNamed:@"gift.png"];

  CCPhysicsBody *giftBody  = [CCPhysicsBody
    bodyWithRect:(CGRect){CGPointZero,
    giftSprite.contentSize} cornerRadius:0];

  giftBody.type = CCPhysicsBodyTypeDynamic;

  giftSprite.physicsBody = giftBody;
  giftSprite.position = position;


  [_physicsWorld addChild:giftSprite];


}

Here, instead ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required