Book Image

Sparrow iOS Game Framework Beginner's Guide

By : Johannes Stein
Book Image

Sparrow iOS Game Framework Beginner's Guide

By: Johannes Stein

Overview of this book

Table of Contents (20 chapters)
Sparrow iOS Game Framework Beginner's Guide
Credits
About the Author
About the Reviewers
www.PacktPub.com
Preface
Afterword
Index

Time for action – avoiding hardcoded values


To separate and load our gameplay-relevant data, we need to follow these steps:

  1. Add a new file called gameplay.json in the Resources folder with the following content:

    {
        "hitpoints": 100,
        "damage": 25,
        "battlefield": {
            "enemy": {
                "x": 100,
                "y": 100
            },
            "pirate": {
                "x": 300,
                "y": 100
            }
        }
    }
  2. Open the Ship.h file.

  3. Add a property called maxHitpoints, as shown in the following line of code:

    @property int maxHitpoints;
  4. Inside the Ship initializer, replace the piece of code where we set hitpoints with the following lines of code:

    self.maxHitpoints = [(NSNumber *) [Assets dictionaryFromJSON:@"gameplay.json"][@"hitpoints"] intValue];
    
    self.hitpoints = self.maxHitpoints;
  5. Inside the hit method, replace the hardcoded damage value to load from the gameplay.json file, as shown in the following code:

    self.hitpoints = self.hitpoints - [(NSNumber *) [Assets dictionaryFromJSON:...