SparrowLogoThis is the second part of a two-part series.  If you have not read part one you can find it here: “Migration of HelloWorld iOS game from Sparrow 1.4 to Sparrow 2.0 – Part I

As a quick recap,  We are migrating Andreas Wålm’s HelloWorld iOS game to Sparrow framework 2.0.  It was originally written using ARC, CocoaPods, Universal, Sparrow 1.4.

Now that we have the new initialization process in place let’s change the Game class.

Change the Game Class

The Game class represents our root game object and is a subclass of SPSprite.  In the original Sparrow 1.4 example the GameController SPStage class creates this root object.  The SPStage is now created automatically by the SPViewController and we no longer need the GameController class.  So how are we going to initialize the display tree?  The SPViewController provides the startWithRoot: (or variant) method.  We simply pass it the Game as a class and as soon as OpenGL completes its set up the viewController creates the class we defined .

The game class is automatically initialized from the SPViewController.  Therefore, we remove the initwithWidth:height: and we do not need the gameWidth or gameHeight properties.  What if you need to pass information to your game class?  You can use the onRootCreated callback of the viewController instance.  However, we still need to define the height and width so we move this into the init method.

Now let us see how this changes the method file.

The Initialization Method

We remove this method and replace it by overriding the init: method of the SPSprite.

The only change is instead of  being provided the gameWidth and gameHeight we decide it.

There are other Game class changes to make but we will cover them in a bit when we look at the Tween changes.

The Juggler

Juggler has moved.  It is no longer accessible from the stage object.  It is now accessible from the Sparrow class.  Change all references to the juggler in all classes.  For example change

to

 The Particle System

The particle system is a class that makes it easy to display particles systems.  Use Particle systems to create special effects in your game.  These effects include stars twinkling, explosions, smoke etc.

The Particle System extension SPParticleSystem used with Sparrow 1.x will not work with Sparrow 2.0.  Download the SPParticleSystem from gitHub and replace the SPParticleSystem files in our project.

The SXNSDataExtensions.h and SXNSDataExtensions.m files are no longer needed so remove it.

Tween Changes

There are several changes to SPTween.

Events

The SP_EVENT_TYPE_TWEEN_COMPLETED event is now covered by the onComplete: block.  The event is no longer available.  You can fire your own event from the block if you have a need such as bubbling. An example of this change is:

Changes to

One of the great things about using blocks is now we no longer need the event handler method onArrivedAtTarget: move this logic into the block itself.

Do this in RocketSprite.m and TitleSprite.m.  In TitleSprite.m I decided to keep the event handler method and call it from the block because we reference the logic from more than one block.

We also need to do this in Game.m but in the Game class we get a ‘retain cycle’ warning.  Do not ignore this warning!  The solution to a ‘retain cycle’ warning is simple, create a weak reference for the member variable used in the block.

 Reverse Property

There are two places where we  set the tween to reverse on every second repetition when looping.  They occur in TitleSprite.m and UFOSprite.m.  This is an easy one line change from

Changes to

 Conclusion

As you can see the process of migrating from Sparrow 1.4 to Sparrow 2.0, for this example, is not that complex.  I have attempted to give a step by step walk-through.  It is possible I have overlooked a couple of things.  If you do notice an oversight please leave a comment or email me and I will get it corrected.

I hope this has helped you understand some of the differences in the Sparrow Framework 2.0.  If you are new to iOS game programming and the Sparrow framework then I hope this provided some insight into the game development process with an updated complete Sparrow 2.0 example.

Thanks to the Gamua team for producing Sparrow and to Andreas Wålm for originating such a great game example.

Subscribe to BehindTheCode.Net today because another great game example is on its way.  

 Fork the complete source code from Github

Read more about Sparrow 2.0

Check out Gamua, the creators of Sparrow 2.0

Visit Andreas Wålm and give him a word of thanks for creating such a great game example