- iOS Game Programming Cookbook
- Bhanu Birani Chhavi Vaishnav
- 809字
- 2021-07-23 20:03:19
Learning the basics of SpriteKit – The FlyingSpaceship tutorial
In this section we will learn and explore basic concepts of SpriteKit. We will also develop a mini game, which will help in understanding the concepts with some robust implementation. The best way to learn SpriteKit is to see it in action.
Getting ready
To build a SpriteKit game, firstly you need to understand the basic structure of a SpriteKit project. You can get started with a starter project having a SKScene
and an SKNode
placed on it. This would equip you with the setup to build your basic game.
How to do it...
To understand the basic concepts of game programming, let's create a new project with the SpriteKit game template with project name FlyingSpaceship
. The project will demonstrate the structure of a SpriteKit project. The end goal of the project is that a Spaceship is visible on the screen and in the upcoming topics we can make it fly.
We will follow some of the same steps we performed in Chapter 1, iOS Game Development and finally add the Spaceship to the screen:
- Start your Xcode and navigate to File | New | Project. Then from the prompt window navigate to iOS | Application | SpriteKit Game and click on Next.
- Fill all the project details in the prompt window and provide
FlyingSpaceship
as the project name with your Organization Name, Devices as iPhone, and Class Prefix asFS
. Click on Next as shown in the following screenshot: - Select a location on drive to save the project and click on Create.
- As a result, the
FSViewController
andFSMyScene
files will be created in the project having aSpaceship.png
file also. The project directory should look something similar to the following screenshot: - Go to the General tab, uncheck Portrait from the device orientation so that the final orientation is landscape.
- Cut the code of typecasting
UIView
toSKView
and presentingFSMyScene
toSKView
from(void)viewDidLoad
ofFSViewController
. - Implement
- (void)viewWillLayoutSubviews
and copy the code fromviewDidLoad
toviewWillLayoutSubviews
. - Finally, the code will look like this:
- Now, let's go to
FSMyScene.m
, remove the default code added in theinit
method and also the method for touch detection. - Make a property for a
SKSpriteNode
called spaceship in the private interface:@interface FSMyScene() @property (nonatomic, strong) SKSpriteNode* spaceShipSprite; @end
- Add this
spaceShipSprite
to theFSMyScene
file in itsinit
method:self.spaceShipSprite = [SKSpriteNode spriteNodeWithImageNamed:@"Spaceship"]; self.spaceShipSprite.position = CGPointMake(self.spaceShipSprite.size.width, size.height/2); [self addChild:self.spaceShipSprite];
The default
Spaceship.png
provided is appropriate, so delete and addSpaceship.png
provided in theResources
folder of the Starter kit. - Now if you run the app, the spaceship doesn't look good on a black background, so give the sky color background color to
FSMyScene
file in itsinit
method.self.backgroundColor = [UIColor colorWithRed:135.0/255.0 green:206.0/255.0 blue:235.0/255.0 alpha:1.0];
So finally we have reached the goal and have placed a spaceship in the sky.
The final
FSMyScene
class looks like this:
In the preceding screenshot, you will observe an update: method
in the .m
file. This method is automatically invoked while rendering each frame on the screen. If the frame rate of the game is 60, then this method will be executed 60 times in a second. Any real-time calculations can be performed in this method, so actions such as calculating the player's location in real time can be handled in this method.
And the starter kit game, FlyingSpaceship
, looks like this:

How it works...
The structure of SpriteKit is fundamentally derived and inherited from the UIKit framework. The operating system offers a smooth transition from UIKit to SpriteKit by just typecasting the UIKit view controller's view to a SpriteKit view called SKView. After this, you are ready to play with the SpriteKit stuff. As shown in the following block diagram, create a scene, add some nodes (that is Sprites as players, background stuff, and so on) to it and you have the game environment built. You can also make the environment more live by applying some actions (rotate, move, scale, and many more) to the nodes added.
Hence, in combination, this scene has different kinds of nodes with some actions applied, that make the basic structure of your SpriteKit and also the game you thought of building.

There's more...
SpriteKit can be used for game development on iOS and OS X platforms. The available graphics hardware of the hosting device is used to render composite 2D images at high frame rates. There are several other features of SpriteKit, which support the following kinds of content, including:
- Sprites that can be of any form such as untextured or textured rectangles
- Text
- Arbitrary CGPath-based shapes
- Video
If you are curious to know more, then visit Apple's developer link https://developer.apple.com/library/ios/documentation/GraphicsAnimation/Conceptual/SpriteKit_PG/Introduction/Introduction.html.
You can also play around with the sample you have just created by trying to change the location of the spaceship and applying various colors to its background.
- DevOps入門與實踐
- 區塊鏈:以太坊DApp開發實戰
- Getting Started with Hazelcast(Second Edition)
- Julia高性能科學計算(第2版)
- Learning Apache Karaf
- 細說Python編程:從入門到科學計算
- QGIS Python Programming Cookbook(Second Edition)
- 動手打造深度學習框架
- Learning C++ by Creating Games with UE4
- Maven for Eclipse
- 創新工場講AI課:從知識到實踐
- Distributed Computing with Python
- Mobile Test Automation with Appium
- ASP.NET 4權威指南
- 瘋狂Ajax講義(第3版)