- 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];
- DevOps:軟件架構(gòu)師行動(dòng)指南
- The Supervised Learning Workshop
- Testing with JUnit
- 控糖控脂健康餐
- 深入淺出Spring Boot 2.x
- Learning ASP.NET Core 2.0
- C語言程序設(shè)計(jì)
- Apache Karaf Cookbook
- INSTANT Django 1.5 Application Development Starter
- Visual FoxPro程序設(shè)計(jì)
- 移動(dòng)界面(Web/App)Photoshop UI設(shè)計(jì)十全大補(bǔ)
- 速學(xué)Python:程序設(shè)計(jì)從入門到進(jìn)階
- Spring Boot+Vue全棧開發(fā)實(shí)戰(zhàn)
- Spring 5 Design Patterns
- 動(dòng)手打造深度學(xué)習(xí)框架