Search code examples
cocos2d-iphoneccsprite

How to make a CCSprite physics enabled


Im working on a iOS GAME. Now I need to create a CCSprite in my function and want it to float into the sky. Here is the code:

CCSprite *corpse =[[CCSprite alloc] initWithImageNamed:@"kid_dead.png"];
corpse.physicsBody.type=CCPhysicsBodyTypeDynamic;
corpse.physicsBody.allowsRotation=true;
corpse.physicsBody.affectedByGravity=false;
corpse.physicsBody.velocity=self.kid.physicsBody.velocity;
[self._physicsNode addChild:corpse];

corpse.physicsBody.velocity=CGPointMake(0.0f,10.0f);

However, it seems like the corpse appears like static and not physics-enabled. Anyone can offer some suggestion? Many thanks:)


Solution

  • You first need to create a physics body and assign it before changing any of its properties:

    CCSprite *corpse = [CCSprite spriteWithImageNamed:@"kid_dead.png"];
    corpse.physicsBody = [CCPhysicsBody bodyWithRadius:10 andCenter:CGPointMake(0, 0)];
    

    You find those functions in the CCPhysicsBody class reference.