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 – loading and saving the last played game


Follow these steps to load and save the current state:

  1. In World.h, declare methods to serialize and deserialize data:

    +(NSDictionary *) serialize;
    +(void) deserialize: (NSDictionary *) dict;
  2. Implement these serializers with the following lines of code:

    +(NSDictionary *) serialize
    {
        return @{
                 @"level": [NSNumber numberWithInt:level],
                 @"gold": [NSNumber numberWithInt:gold],
                 @"damage": [NSNumber numberWithInt:damage],
                 @"hitpoints": [NSNumber numberWithInt:hitpoints]
        };
    }
    
    +(void) deserialize: (NSDictionary *) dict
    {
        level = [(NSNumber *) dict[@"level"] intValue];
        gold = [(NSNumber *) dict[@"gold"] intValue];
        damage = [(NSNumber *) dict[@"damage"] intValue];
        hitpoints = [(NSNumber *) dict[@"hitpoints"] intValue];
    }
  3. In MainMenu.m, add World.h to the import section and update the initializer:

    buttonContinue.x = (Sparrow.stage.width - buttonContinue.width) / 2;
    buttonContinue...