Principle
Implementation
Errata
There is an error in the "implemented" video, as shown below:
An error occurred while moving the background. When you look at the red part, the subscript of the second image is 1 when the near-scene is returned.
If arrbg [0]. position. x + arrbg [0]. Frame. width <Speed {
Arrbg [0]. position. x = 0
Arrbg [1]. position. x = arrbg [0]. Frame. Width
}
Key points:
What is parallax scrolling:
Parallax scrolling refers to moving multi-layer backgrounds at different speeds to form a three-dimensional motion effect and bring a very good visual experience.
How to implement:
The first is the background, which consists of two backgrounds. The near-point is the green hillside, and the far-away is the tree. In the move method, we provide a platform moving speed of close-to-scene 1/5. To the platform's mobile speed of vision 1/20. The parallax scroll is formed.
Code:
Import spritekit // inherits from sknodeclass Background: sknode {// near background array var arrbg = [skspritenode] () // background array of distant trees var arrfar = [skspritenode] () // constructor Init () {// execute the constructor super of the parent class. init () // texture of the distant background var fartexture = sktexture (imagenamed: "background_f1") // The distant background consists of three seamless graphs var farbg0 = skspritenode (texture: fartexture) farbg0.anchorpoint = cgpointmake (0, 0) farbg0.position. y = 150 var farbg1 = skspritenode (texture: fartexture) farbg1.anchorpoint = cgpointmake (0, 0) farbg1.position. X = farbg0.frame. width farbg1.position. y = farbg0.position. Y var farbg2 = skspritenode (texture: fartexture) farbg2.anchorpoint = cgpointmake (0, 0) farbg2.position. X = farbg0.frame. width * 2 farbg2.position. y = farbg0.position. Y self. addchild (farbg0) self. addchild (farbg1) self. addchild (farbg2) arrfar. append (farbg0) arrfar. append (farbg1) arrfar. append (farbg2) // near background texture var texture = sktexture (imagenamed: "background_f0") // near background consists of two seamlessly connected graphs var bg0 = skspritenode (texture: texture) bg0.anchorpoint = cgpointmake (0, 0) var Bg1 = skspritenode (texture: texture) bg1.anchorpoint = cgpointmake (0, 0) bg1.position. X = bg0.frame. width bg0.position. y = 70 bg1.position. y = bg0.position. Y self. addchild (bg0) self. addchild (Bg1) arrbg. append (bg0) arrbg. append (Bg1)} // move function func move (speed: cgfloat) {// cyclically traverse the near background and perform X coordinate displacement for BG in arrbg {BG. position. x-= speed} // determines whether the first background image is completely removed from the scene. If it is removed, the background image in the near future is reset if arrbg [0]. position. X + arrbg [0]. frame. width <Speed {arrbg [0]. position. X = 0 arrbg [1]. position. X = arrbg [0]. frame. width} // cyclically traverse the background and perform X-coordinate displacement for far in arrfar {far. position. x-= speed/4} // determines whether the first background image is completely removed from the scene. If it is removed, the background image in the distance is reset if arrfar [0]. position. X + arrfar [0]. frame. width <speed/4 {arrfar [0]. position. X = 0 arrfar [1]. position. X = arrfar [0]. frame. width arrfar [2]. position. X = arrfar [0]. frame. width * 2 }}}
Project file address
Http://yun.baidu.com/share/link? Consumer id = 3824235955 & UK = 541995622
Swift game practice-game preview of the parkfun pandatv series 00 01 create a project import materials 02 create a pandatv 03 animation 04 create a pandatv hop and roll action 05 on the pedaling platform 06 platform and platform factory 07 platforms of mobile 08 generate a steady stream of mobile platforms 09 remove platforms outside of scenarios