The number of iOS app developers using SpriteKit for making custom mobile games is increasing at a rapid clip. For coders who wish to start using SpriteKit, we have here shared a few important guidelines.
SpriteKit would easily feature among the most popular animation and graphics rendering tools among iOS app and game developers worldwide. The top-notch user-friendliness of the tools is one of the key reasons behind its enormous popularity – apart from its obviously vast set of customized features. The learning curve of SpriteKit is far from being steep, and all that newbies require is a bit of guidance, while working with it. If you are planning to make an iPhone game with SpriteKit, the following tips would come in handy:
- Initiate Xcode – It would be advisable to install the latest beta of Xcode 6.3 first. Once the installation is complete, you will have to start up the built-in Game Template (coded with Swift). Doing so is easy enough: click File → New → Project, and navigate to iOS → Application → Game. Once you have clicked on ‘Next’, a dialog box – prompting you to enter the Project name, programming language, devices, etc. will appear.
- Be familiar with SKAction spins – Next, the mobile game development experts working with SpriteKit will find that a single-scene app (with sample text area) has been automatically created by the built-in Xcode game template. Spin actions, a central component of basic Sprite animations, can now be made – with the help of SKAction (check the label node in MyScene.m). There are two different components of SKAction – the first for single radian (180 degrees) spins, and the other for infinite loops. Check out how both of these work.
- For large games, optimize the Tree Content – In theory, there is no need to remove any of the nodes from the parent ‘Tree’ in SpriteKit – irrespective of how many ‘children’ it already contains. However, coders from leading iOS game development companies opine that deletion of unnecessary nodes speeds up the coding process, and minimizes the chances of errors. Broadly speaking, if a node is not related to any action, or is not serving as a physics engine related to the actual gameplay – it is best to remove it from the Tree. The same goes for nodes that are not likely to be rendered anytime soon. Remember, you need to do this only if you are making a relatively large iOS game.
- Use particles and atlases – First-time iOS app developers might not be aware of this feature. SpriteKit comes with a wide range of atlases and gaming particles embedded to it, and they can be used to add a high level of customization to games. What’s more – even videos can be considered and deployed as sprites. There are several interesting masks and image effects that can be used too.
- Get rid of GameScene.sks – When you graduate to making mobile games that require visual creation of sprites and scenes, GameScene.sks would be necessary. For starting out with test apps on SpriteKit though, manual programming is a way simpler option. Move it to Trash (when prompted), and then, start working on the Gameviewcontroller.swift playground. Make sure that, under ‘Deployment’, the boxes next to ‘Landscape Right’ and ‘Landscape Left’ are checked, while the one against ‘Portrait’ is unchecked.
- Add your texture(s) – The groundwork is done, and mobile app and game developers can now start adding textures to their programs. These textures double up as the coders’ own sprites as well. Find out how you can use the components of image files to create textures. Name the texture/sprite and add an (arbitrary) position to it. You will need it later to add to a game scene. Repeat the process for creating multiple textures.
- Create the graphical content with care – Even small mistakes at this stage can lead to crashes in the node trees/scenes. Professionals from the mobile game development industry advise new users to avoid directly adding either heavy physics bodies, or any of the content nodes (creating content layers with extra SKNode objects is a better option). Nesting the effect nodes and the clipping nodes inside the node tree can be problematic as well (especially when you are creating a complex iOS game). The particle birth rates (during the creation of emitter effects) should be maintained at low levels. This will minimize risks of overloading during particle animation and rendering.
- Add your sprite to a SpriteKit scene – You know how to create textures/sprites, and now it’s time to actually use them on the main SpriteKit scene. To do this, you will have to call the ‘spritenodewith ImageNamed’ method, and then pass the name of the image (which you had saved earlier while creating the texture) through it. Then you will have to add the ‘addChild’ function to the scene, and specify the position coordinates of your game character (for instance, a fairy). In case the character is not visible, check for errors in the viewDidLoad (in ViewController.m) method.
- Incorporate Actions to the gameplay – The three action functions available in SpriteKit add to the convenience and customization options of this game development tool. You can add ‘SKAction.removeFromParent ()’ to remove characters from a scene, ‘SKAction.moveTo ()’ to make a character move towards the left of the scene (remember to specify the duration of movement as well), and ‘SKAction.sequence (_:)’ to clump together an action sequence/loop. You need to ensure that the characters appear lively, and more importantly, keep coming. That’s what will make the gameplay interesting.
- Use physics system simulations – Adding motions and special effects (pixie dust, anyone?) to game components is all very well, but do not miss out on the physics effects that can be seamlessly added in the SpriteKit tool. Create a self.physicsBody method, and mention the screen edge boundaries in it (this would ensure that sprites do not suddenly go out of the scene). After that, create a physics system for each sprite that you have created and added. From displaying gravitational effects, to the impact of particle collisions – in-game physics in SpriteKit can make iOS games a lot more realistic and engaging.
- Save game levels in scene archives – SpriteKit offers complete support to creation and maintenance of archives. This, in turn, makes it possible for game developers to: a) save the different game levels as scene node archives, and b) create separate archives for the all nodes that have been pre-configured. The game assets can be created by adding the Property list (which contains all saved SpriteKit data). While referencing nodes, use unique name properties instead of simple hardcoding.
- A bit about animations – Let’s turn our attentions towards the process of creating game animations via SpriteKit here. Note that each and every action is defined as a separate object. The SKAction class defines these objects, and when the actions are executed (in the nodes), the animation frames are processed. If you wish to override preset methods and/or add personalized processes per-frame, that can also be done by using scenes. Keep in mind, while certain actions need several animation frames, others can be executed within a single frame.
- Make the subclasses compatible – In SpriteKit, NSCopying and NSCoding protocols are supported by the node classes. App and game developers, while introducing new instances in subclasses, need to ensure that the latter contain these behaviors as well. For both OS X and the iOS platforms, there are custom event-handling codes that have to be implemented. Ideally, create your own calling conventions, to avoid confusions/errors during rendering.
- Using blend mode and viewing diagnostic messages – In case any form of opaque content is being used in your sprite(s), you will have to use a separate ‘blend mode’ – SKBLendModeReplace. In general, the alpha blend method is used for blending sprite contents and particles. The SKView class in the game development tool also allows users to check diagnostic messages. If you feel that your game is laggy, external tools – like OpenGL – can also be used to check the timings of the separate components of the game.
To locate the precise coordinates of a user-touch, the SKNode of SpriteKit has two extremely handy methods – previousLocationInMode (_:) and locationInMode (_:). Separate loading classes are required for storing textures that are called only once (when the game starts). Once you have made your SpriteKit/Swift game, test it on actual devices (iOS or OS X, as the case may be) – and find out if there are any CPU/GPU compatibility issues. SpriteKit is an excellent tool for making iOS games – and you can start developing as soon as you get a hang of working with it.