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

Adding infinite scrolling

Now we are all ready with our spaceship. It's time to add some more in the game. So our next target is to add infinite scrolling to the game, so that we can make our spaceship move infinitely in space. In this recipe, we will be learning about the ways to add infinite scrolling to games.

Getting ready

For an infinite scrolling background you need to know about the anatomy of SpriteKit shown earlier. You should be aware of the rendering loop, how in a particular frame the update method functions, and how SKScene evaluates the actions and physics simulation thereby rendering all the stuff in the SKView. Now using this loop, you implement sky scrolling infinitely giving the feel of a spaceship flying.

How to do it...

Now is the time for action; perform the following steps to add the infinite scrolling background to your game.

  1. Import the SpaceBackground.png file provided in the Resources folder.
  2. Add a function in FSMyScene for initializing the infinite background.
  3. In order to enable scrolling, we have to add two identical background sprite nodes one after the other.
  4. In the function, run a for loop for two background nodes specifying the position, a name (tag), and then adding to SKMyScene.
  5. Hence, the initalizingScrollingBackground function looks like this:
    - (void)initalizingScrollingBackground
    {
        for (int index = 0; index < 2; index++)
        {
            SKSpriteNode *spaceBGNode =
            [SKSpriteNode spriteNodeWithImageNamed:@"SpaceBackground.png"];
            {
                spaceBGNode.position =
                CGPointMake(index * spaceBGNode.size.width, 0);
                spaceBGNode.anchorPoint = CGPointZero;
                spaceBGNode.name = @"SpaceBG";
                
                [self addChild:spaceBGNode];
            }
        }
    }
  6. Add this method into the init method and also move the code to add a spaceship into a different method called addSpaceship.

    Note

    In game programming, the layers of objects are made by the sequence they are added in. So for the preceding example, the Spaceship should be added after SpaceBackground giving an appearance that the ship is above the background.

The sequence of the views presented on the screen can be changed by altering their z coordinate; the view having highest z coordinate will always be on the top. This means we can explicitly define which layer we want to keep on top and which we want to keep at the bottom, which is explained in the following steps:

  1. The initial background is added but it's not scrolling. This could be accomplished by the update method discussed in the Anatomy of game projects recipe.
  2. For this, some math is required to implement this functionality. Build some inline functions and constants to be used for infinitely moving the background. This is the code needed:
    static const float SPACE_BG_VELOCITY = 100.0;
    static inline CGPoint CGPointAdd(const CGPoint a, const CGPoint b)
    {
        return CGPointMake(a.x + b.x, a.y + b.y);
    }
    static inline CGPoint CGPointMultiplyScalar(const CGPoint a, const CGFloat b)
    {
        return CGPointMake(a.x * b, a.y * b);
    }
  3. Just add these line of code preceding to the implementation of FSMyScene.
  4. Now the real way to do it is, in the update method, iterate all nodes added in FSMyScene, identify the SpaceBackground node by its name assigned in the initialization function, and adjust its position to enable infinite scrolling. Do all of this in a function named moveSpaceBackground.
    - (void)moveSpaceBackground
    {
        [self enumerateChildNodesWithName:@"SpaceBG"
                               usingBlock: ^(SKNode *node, BOOL *stop)
         {
             SKSpriteNode * spaceBGNode = (SKSpriteNode *) node;
             
             CGPoint bgVelocity = CGPointMake(-SPACE_BG_VELOCITY, 0);
             
             CGPoint amtToMove = CGPointMultiplyScalar(bgVelocity,self.diffTime);
             
             spaceBGNode.position = CGPointAdd(spaceBGNode.position, amtToMove);
             
             //Checks if Background node is completely scrolled of the screen, if yes then put it at the end of the other node
             
             if (spaceBGNode.position.x <= -spaceBGNode.size.width)
             {
                 spaceBGNode.position =
                 CGPointMake(spaceBGNode.position.x + spaceBGNode.size.width*2,
                             spaceBGNode.position.y);
             }
         }];
    }
  5. Lastly, call this method every time in the update method of the game scene. After that, you should see the spaceship flying in the sky with some nice white clouds.

How it works...

Implementation of infinite scrolling is divided into three parts. We need to follow the following steps to accomplish infinite scrolling for our game:

  • Initializing the SpaceBackground: Two space backgrounds are added one after the other so that they are moved at the same time to give a feel of an infinite scrolling background.
  • SpaceBackground move code: Here, a block method of SKScene is used to iterate all nodes of the scene.
    [self enumerateChildNodesWithName:@"SpaceBG"
                               usingBlock: ^(SKNode *node, BOOL *stop)
         {
    
         }];

In this iteration, the SpaceBGNode is identified by its name so that its position can be updated.

SKSpriteNode * spaceBGNode = (SKSpriteNode *) node;

The amount of distance to be moved is calculated using the CGPointMultiplyScalar inline function that is fed with the constant value SPACE_BG_VELOCITY and the difference of time obtained from the update method in each frame.

CGPoint bgVelocity = CGPointMake(-SPACE_BG_VELOCITY, 0);
CGPoint amtToMove = CGPointMultiplyScalar(bgVelocity,self.diffTime);

After that, the calculated distance is added in the current position of SpaceBGNode.

spaceBGNode.position = CGPointAdd(spaceBGNode.position, amtToMove);

The last but the most important step to enable scrolling, is to set the position of SpaceBGNode to the right edge of the screen whenever it reaches the left edge of the screen.

if (spaceBGNode.position.x <= -spaceBGNode.size.width)
{
        spaceBGNode.position =
             CGPointMake(spaceBGNode.position.x + spaceBGNode.size.width*2,
                         spaceBGNode.position.y);
  }

The next task is to update each frame to move it infinitely over the scene. Now to make it move regularly, the moveSpaceBackground method is called in the update method of FSMyScene in each frame.

- (void)update:(CFTimeInterval)currentTime
{
    /* Called before each frame is rendered */
    self.diffTime = currentTime - self.lastUpdatedTime;
    
    self.lastUpdatedTime = currentTime;
    
    [self moveSpaceBackground];
}

The update loop will be executed in every frame. So to move our background in every step, we have called the moveSpaceBackground method inside the update loop. Using this approach of infinite scrolling, we can also implement parallax gaming, which is very common nowadays. In parallax scrolling game, there will be background and player in separate layers and they will be moving at the same time at different speeds. This will give the user a perception of some real-time movement of the player against the background.

主站蜘蛛池模板: 林周县| 繁峙县| 台中市| 皋兰县| 通化县| 政和县| 南阳市| 汉川市| 武宁县| 沅江市| 龙山县| 邵阳市| 松滋市| 遂川县| 广灵县| 彭泽县| 杭州市| 宁城县| 台东县| 淮安市| 城步| 泸州市| 宁远县| 诸暨市| 都安| 庆云县| 桐庐县| 达孜县| 芦溪县| 桓仁| 盈江县| 巫山县| 凌源市| 吴忠市| 荆州市| 昭苏县| 潮安县| 南开区| 临湘市| 双江| 沈丘县|