- iOS Game Programming Cookbook
- Bhanu Birani Chhavi Vaishnav
- 976字
- 2021-07-23 20:03:20
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.
- Import the
SpaceBackground.png
file provided in theResources
folder. - Add a function in
FSMyScene
for initializing the infinite background. - In order to enable scrolling, we have to add two identical background sprite nodes one after the other.
- In the function, run a
for
loop for two background nodes specifying the position, a name (tag), and then adding toSKMyScene
. - 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]; } } }
- Add this method into the
init
method and also move the code to add a spaceship into a different method calledaddSpaceship
.
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:
- 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.
- 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); }
- Just add these line of code preceding to the implementation of
FSMyScene
. - Now the real way to do it is, in the update method, iterate all nodes added in
FSMyScene
, identify theSpaceBackground
node by its name assigned in the initialization function, and adjust its position to enable infinite scrolling. Do all of this in a function namedmoveSpaceBackground
.- (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); } }]; }
- 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.
- 跟小海龜學Python
- Instant Typeahead.js
- Eclipse Plug-in Development:Beginner's Guide(Second Edition)
- 零基礎輕松學SQL Server 2016
- R大數據分析實用指南
- 機器學習與R語言實戰
- 愛上micro:bit
- 零代碼實戰:企業級應用搭建與案例詳解
- ASP.NET Web API Security Essentials
- 零基礎看圖學ScratchJr:少兒趣味編程(全彩大字版)
- Learning Image Processing with OpenCV
- ASP.NET Core and Angular 2
- Java面向對象程序設計教程
- Mastering ASP.NET Web API
- JavaScript Mobile Application Development