Before we add any other objects, we need to prepare the scene to use physics.
Once again, I have created a new project for this chapter. You know the standard operating procedure by now. So, create a new project and make the necessary changes to the project.
In the MainScene.h
file, we will add a new variable of the CCPhysicsNode
type named _physicsWorld
. All the physics objects that we create in a game must be added to this henceforth. We will run the following:
CCPhysicsNode *_physicsWorld;
Next, in the init
function, we will add the following:
winSize = [[CCDirector sharedDirector]viewSize]; CGPoint center = CGPointMake(winSize.width/2, winSize.height/2); self.contentSize = winSize; CCSprite* scenery = [CCSprite spriteWithImageNamed:@"scenery.png"]; scenery.position = center; [self addChild:scenery]; _physicsWorld = [CCPhysicsNode node]; _physicsWorld.gravity = ccp(0,-100); _physicsWorld.debugDraw = true; [self addChild:_physicsWorld...