官术网_书友最值得收藏!

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:

  1. Create the SpriteKit Game Template from Xcode.
  2. A default ViewController and scene will be created for you.
  3. Typecast the ViewController view to SKView by enabling the showsFPS and showsNodeCount properties to YES.
    // Configure the view.
    SKView * skView = (SKView *)self.view;
    skView.showsFPS = YES;
    skView.showsNodeCount = YES;
  4. Create a scene using a class method of SKScene specifying the size of the scene also, and then present that scene on the SKView 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.

  5. 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 the SKSceneScaleModeAspectFill mode is used so that it fits as per the aspect ratio of the SKView:
        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];
主站蜘蛛池模板: 信阳市| 文昌市| 略阳县| 万州区| 永宁县| 共和县| 葵青区| 静海县| 公主岭市| 东源县| 高陵县| 宕昌县| 苍南县| 西乌珠穆沁旗| 景泰县| 上杭县| 米易县| 万盛区| 惠东县| 都安| 佛坪县| 崇州市| 义乌市| 湟中县| 自贡市| 芦溪县| 峨边| 海南省| 临朐县| 道真| 洛阳市| 米泉市| 禹州市| 苏尼特右旗| 金阳县| 秦皇岛市| 九龙城区| 随州市| 林西县| 邵阳市| 永寿县|