Swift game practice-create a platform and a platform Factory

Source: Internet
Author: User

 

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.