In this section, let's take a look at the principles of the random length pedaling platform.
Key points:
Platform
Our platform class inherits from sknode, so that it can be added to other nodes and displayed in the scene.
It has a method to create a platform, which receives an array containing skspritenode. Concatenates objects in the array horizontally to form a complete platform. Calculate the platform width.
onCreate(arrSprite:[SKSpriteNode]){ for platform in arrSprite{ platform.position.x=self.width self.addChild(platform) self.width += platform.size.width }}
Platform Factory
This class controls the generation of platform classes. At the same time, there are three constants that record the texture of the platform, so you do not need to load local resource files every time. It plays a role in optimization.
let textureLeft = SKTexture(imageNamed: "platform_l")let textureMid = SKTexture(imageNamed: "platform_m")let textureRight = SKTexture(imageNamed: "platform_r")
The createplatform method is an external call used to generate a platform.
func createPlatform(midNum:UInt32,x:CGFloat,y:CGFloat){ let platform = Platform(); let platform_left = SKSpriteNode(texture: textureLeft) platform_left.anchorPoint = CGPointMake(0, 0.9) let platform_right = SKSpriteNode(texture: textureRight) platform_right.anchorPoint = CGPointMake(0, 0.9) var arrPlatform = [SKSpriteNode]() arrPlatform.append( platform_left ) platform.position = CGPointMake(x, y) 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)}
Performance in scenarios
Finally, we declare a platform factory class in the scenario.
@lazy var platformFactory = PlatformFactory()
And create a platform
self.addChild(platformFactory)platformFactory.createPlatform(1, x: 0, y: 200)
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 project import materials 02 creation of pandatv 03 animation 04 pandatv hop and roll action 05 How the pedaling platform is made