- iOS Game Programming Cookbook
- Bhanu Birani Chhavi Vaishnav
- 776字
- 2021-07-23 20:03:20
Understanding scenes, nodes, and sprites
The whole game is organized into scenes, which have the content represented by SKScene
objects.
A scene is an entity that holds all the content, that is, nodes and sprites that are to be rendered. It also implements the setup or anatomy of content processing and updating each frame.
The SKScene
class is a subclass of SKNode, which is the fundamental building block of SpriteKit. Every entity in SpriteKit is inherited or derived from the node (SKNode). So SKScene
is the root node for other nodes, which are used to populate the content over a scene.
Similar to UIKit, each node's position is specified according to the coordinate system of its parent. A node also has the basic properties that a content item or entity should have such as moving, rotating, scaling, fading out, and many more. And most important, all node objects are responder objects that respond to the delegates of UIResponder. This is used to detect input touches to the scene for moving objects and some other stuff depending on one's gameplay.
Now, sprites are represented by SKSpriteNode objects. They are nodes with images on them. We can specify content or a textured image to them as we have to make some player or enemies in a game. SKSpriteNode is also inherited from SKNode. Additionally, its content can be changed and animated. The sprites are created and added on scenes with some actions to make the game scene more alive.
Getting ready
To understand these elements of SpriteKit, we need to create a blank project just as we did in the starter project of this chapter. As in this starter kit, a basic SKScene
and SKNode
are shown. So we will now go through these terminologies and their sample code snippets.
How to do it...
As we did in the starter kit, follow the same steps to create a SKScene
and add a SKSpriteNode
method to it:
- Create the SpriteKit Game Template from Xcode.
- A default ViewController and scene will be created for you.
- Typecast the ViewController view to
SKView
by enabling theshowsFPS
andshowsNodeCount
properties toYES
.// Configure the view. SKView * skView = (SKView *)self.view; skView.showsFPS = YES; skView.showsNodeCount = YES;
- Create a scene using a class method of
SKScene
specifying the size of the scene also, and then present that scene on theSKView
typecasted before.// Create and configure the scene. SKScene * scene = [SKScene sceneWithSize:skView.bounds.size]; scene.scaleMode = SKSceneScaleModeAspectFill; // Present the scene. [skView presentScene:scene];
All this should be done in the
- (void)viewWillLayoutSubviews
method. - Now we have to add some sprite to the scene we created earlier. Create an object of SKSpriteNode by calling a class method and specifying an image of the sprite. Now assign the location where it has to be placed and lastly add it to the scene.
SKSpriteNode * spriteNode = [SKSpriteNode spriteNodeWithImageNamed:@"Spaceship.png"]; spriteNode.position = CGPointMake(100,100); [self addChild:spriteNode];
How it works...
As explained in the structural block diagram of the How it works... section of the Learning the basics of SpriteKit – The FlyingSpaceship tutorial recipe, it's deeply linked with the UIKit framework. For building a game, we should have an environment, which is our scene, and some entities visible over the environment, which are the sprites. So to make it work, or should I say to make something visible on the screen, an environment (that is, scene) is created and on it entities (that is, sprites) are added, as follows:
- When we typecast UIView in to
SKView
, we enter the arena of SpriteKit:SKView * skView = (SKView *)self.view;
- For debugging purposes, we enable two Boolean parameters to show FPS (Frames per second) and NodesCount (the number of nodes added to the scene):
skView.showsFPS = YES; skView.showsNodeCount = YES;
- When creating a scene, we need to specify the size of the scene that is exactly the content size and the scale mode so that the scene fits in
SKView
(that is, scale perspective), here theSKSceneScaleModeAspectFill
mode is used so that it fits as per the aspect ratio of theSKView
:SKScene * scene = [SKScene sceneWithSize:skView.bounds.size]; scene.scaleMode = SKSceneScaleModeAspectFill;
- To make the scene content visible on the view, we present the scene on
SKView
:// Present the scene. [skView presentScene:scene];
- Now about how the sprites work. A sprite object is created by a class method that instantiates a node having an image as its content:
SKSpriteNode * spriteNode = [SKSpriteNode spriteNodeWithImageNamed:@"Spaceship.png"];
- The following line of code specifies the position where exactly the sprite needs to be placed:
spriteNode.position = CGPointMake(100,100); Lastly, to make the sprite visible, it is added to
SKScene
as a child: [self addChild:spriteNode];
- 流量的秘密:Google Analytics網站分析與優化技巧(第2版)
- Java 9 Concurrency Cookbook(Second Edition)
- Vue.js前端開發基礎與項目實戰
- 體驗設計原理:行為、情感和細節
- PHP+MySQL網站開發技術項目式教程(第2版)
- PyTorch自然語言處理入門與實戰
- AngularJS深度剖析與最佳實踐
- Mastering Rust
- 從Excel到Python:用Python輕松處理Excel數據(第2版)
- Learning Continuous Integration with TeamCity
- 持續集成與持續交付實戰:用Jenkins、Travis CI和CircleCI構建和發布大規模高質量軟件
- Learning Modular Java Programming
- 人工智能算法(卷1):基礎算法
- 計算機應用基礎項目化教程
- Python數據可視化之美:專業圖表繪制指南(全彩)