Book Image

iOS Game Programming Cookbook

Book Image

iOS Game Programming Cookbook

Overview of this book

Table of Contents (19 chapters)
iOS Game Programming Cookbook
Credits
About the Authors
About the Reviewers
www.PacktPub.com
Preface
Index

Adding real-world simulation


Now we will be adding some real simulation in the game. We will add more physics bodies and make them interact with each other. This will help us to understand the physics interaction between various physics objects.

How to do it...

Now we have the infinite bouncing ball in place. To add more fun to the game, let us add some more elements to the it.

  1. First we will add the static block to the game. To accomplish this, add the following line of code at the end of the initWithSize method:

    SKSpriteNode* block = [[SKSpriteNode alloc] initWithImageNamed: @"block.png"];
    block.name = paddleCategoryName;
    block.position = CGPointMake(CGRectGetMidX(self.frame), block.frame.size.height * 0.6f);
    [self addChild:block];
    block.physicsBody = [SKPhysicsBody bodyWithRectangleOfSize:block.frame.size];
    block.physicsBody.restitution = 0.1f;
    block.physicsBody.friction = 0.4f;
    // make physicsBody static
    block.physicsBody.dynamic = NO;

    First we create the block sprite. After that we associate...