In the previous section, we wrote something about the crazy platform. The so-called god will first make it crazy to make it perish. Crazy is not a good thing, so we need to take some measures, such as removing platforms other than scenes. If one day you feel that your boss behave well, it's hard to justify it. For example, if there is nothing to do and you have nothing to do to find something to make you crazy to work overtime, you should be careful at this time ...... Haha.
Key points:
How to determine the platform removal scenario:
Since our platform is generated sequentially one by one, we only need to determine whether to remove the first platform of the array, that is, the element with the subscript of 0. How can we determine the removal scenario? Since the platform's anchor is on the leftmost side, you only need to determine whether the coordinate of the platform is smaller than the negative value of the platform's width. All of this is done in the move method.
if platforms[0].position.x < -platforms[0].width { platforms[0].removeFromParent() platforms.removeAtIndex(0)}
At this time, the complete code of our platform factory class should be like this, and the important code has been Roughened up.
Import Export platformfactory: sknode {Let textureleft = sktexture (imagenamed: "success") Let plaintext = sktexture (imagenamed: "platform_m") Let textureright = sktexture (imagenamed: "platform_r ") vaR platforms = [platform] () var scenewidth: cgfloat = 0 var delegate: protocolmainscene? Func createplatformrandom () {Let midnum: uint32 = arc4random () % 4 + 1 Let gap: cgfloat = cgfloat (arc4random () % 8 + 1) Let X: cgfloat = self. scenewidth + cgfloat (midnum * 50) + gap + 100 let Y: cgfloat = cgfloat (arc4random () % 200 + 200) createplatform (midnum, X: X, Y: Y )} func createplatform (midnum: uint32, X: cgfloat, Y: cgfloat) {Let platform = platform () platform. position = cgpointmake (x, y) Let platform_left = Skspritenode (texture: textureleft) Labels = cgpointmake (0, 0.9) Let platform_right = skspritenode (texture: textureright) Labels = cgpointmake (0, 0.9) var arrplatform = [skspritenode] arrplatform. append (platform_left) for I in 1... midnum {Let platform_mid = skspritenode (texture: texturemid) platform_mid.anchorpoint = cgpointmake (0, 0.9) arrplatform. append (Platform_mid)} arrplatform. append (platform_right) platform. oncreate (arrplatform) self. addchild (Platform) platforms. append (Platform) // general formula: length of the generated Platform + x coordinate of the platform-width of the main scenario delegate ?. Ongetdata (platform. Width + X-scenewidth )}Func move (speed: cgfloat ){For P in platforms {P. position. x-= speed} If platforms [0]. position. x <-platforms [0]. width {platforms [0]. removefromparent () platforms. removeatindex (0)}}}
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, mobile 08, generate a steady stream of Mobile Platforms