Swift language Combat Promotion-9th chapter game-Running cool panda-5-6 pedaling platform how to refine

Source: Internet
Author: User
Tags addchild

Original: Swift language Combat Promotion-9th chapter game-Running cool panda-5-6 pedaling platform is how to refine

In the game, there are many points to fly to the platform, this platform varies. It is tedious to create a picture of each length. In fact, we only use 3 images. The platform, the middle part of the platform, the right side of the platform, respectively. The key is the middle part of the platform, where the two middle sections are well connected so that you can create platforms of different lengths as long as you increase the number of intermediate parts. So how do you make this picture? Let's find a complete picture of the platform

Then cut out the middle part.

At this time, we can find that the two pieces of the middle can be seamlessly together. Then we'll put it together. And the middle part of the piece, we copy the whole piece, we found that we can seamlessly splice without flipping, so that the middle part of the platform is completed. The right part of the platform only needs to be flipped horizontally with the left part of the platform in the diagram.

In this way, we get all the platform files.

9 . 6 creating platform classes and Platform factory classes

Starting with this section, we're going to start coding the relevant classes of the platform. Let's start by creating a platform class. Platform inherits from Sknode. This class does not need to load the texture to implement the platform assembly function, so it inherits from Sknode. Then we need to achieve wide width and high height:

Wide var width:cgfloat = 0.0//high var height:cgfloat = 10.0

Then we need to write a OnCreate method to assemble the parts of the platform. OnCreate accepts an array of type Skspritenode. And the elements within the array are sorted horizontally in order to join the instances in the current class.

Func onCreate (Arrsprite:[skspritenode]) {//By accepting Skspritenode array to create platform for platform in Arrsprite {// X-coordinate of the platform part with current width platform.position.x = self.width//load Self.addchild (platform)//update width Self.width + = Platform.size.width}}

The complete code is as follows:

Platform class import Spritekitclass platform:sknode{    //Wide    var width:cgfloat = 0.0    //High    var height:cgfloat = 10.0< C4/>func onCreate (Arrsprite:[skspritenode]) {        //By accepting Skspritenode array to create platform for        platform in Arrsprite {            // X-coordinate of the platform part with current width            platform.position.x = self.width            //Load            Self.addchild (platform)            //Update width            Self.width + = Platform.size.width}}}    

Next we're going to write a platform for the factory class, which is responsible for generating platform parts and then passing it to the Platform class for assembly. When coding, we should design different classes according to function to be responsible for different functions. This has a clear structure and facilitates future extension changes.

The factory class is named PlatformFactory, which also inherits from Sknode. In the factory class, the texture of the platform is stored.

Define platform left texture let Textureleft = Sktexture (imagenamed: "platform_l")//define Platform intermediate texture let Texturemid = Sktexture (imagenamed: " Platform_m ")//define platform right texture let Textureright = Sktexture (imagenamed:" Platform_r ")

We also need to define an array to store the generated platform:

Define an array to store the assembled platform var platforms = [Platform] ()

Now start writing a method for the build platform, Createplatform, which receives four parameters. Israndom, a platform used to determine whether to generate random locations, is temporarily unavailable, but is prepared for expansion later. Minnum is the creation of several intermediate parts. X and y are the coordinates of the position.

Createplatform This method, the platform parts array is built into the platform and assembled according to the requirements. Finally put into the platform array.

The Platform factory class is not only responsible for building the platform, but also a container itself, so that all platforms can be unified control. The complete code is as follows:

Import Spritekitclass Platformfactory:sknode {//define platform left texture let Textureleft = Sktexture (imagenamed: "platform_l") Define platform Intermediate texture let Texturemid = Sktexture (imagenamed: "platform_m")//define platform right texture let Textureright = sktexture (imageName D: "Platform_r")//define an array to store the assembled platform var platforms = [Platform] () func createplatform (israndom:bool,midnum:        Uint32,x:cgfloat,y:cgfloat)->platform{//Declare a platform class that is used to assemble the platform.        var platform = platform ()//build platform Left part let Platform_left = Skspritenode (texture:textureleft)//Set Center point Platform_left.anchorpoint = Cgpoint (x:0, y:0.9)//build platform on the right part let Platform_right = Skspritenode (textu         Re:textureright)//Set Center point platform_right.anchorpoint = Cgpoint (x:0, y:0.9)//Declare an array to hold the parts of the platform var arrplatform = [Skspritenode] ()//Add left part to part array arrplatform.append (PLATFORM_LEFT)// Depending on the parameters passed in, it is decided to assemble the intermediate parts of several platforms//and then add the intermediate parts to the part array for I in 1...midNum {Let Platform_mid = Skspritenode (texture:texturemid) platform_mid.anchorpoint = Cgpoint (x:0, y:        0.9) Arrplatform.append (PLATFORM_MID)}//Add the right part to the part array arrplatform.append (platform_right) Passing a part array to Platform.oncreate (arrplatform) platform.name= "platform"//Setting the location of the platform Platform.po Sition = Cgpoint (x:x, y:y)//Put into the current instance Self.addchild (platform)//Add platform to platform array platforms.append (PL Atform) return Platform}}

After I finished the Platform factory class and platform class, we can't wait to test it. We can switch to Gamescene to add code to test it:

Declare a platform factory first:

Lazy var platformfactory = PlatformFactory ()

The platform factory is then added to the current view in the Didmovetoview method.

Self.addchild (PlatformFactory)

This is followed by a line of temporary test code:

Let platform = Platformfactory.createplatform (False, Midnum:1, x:100, y:300)

Run the code to test it, the effect is as follows:

Good, get it done.

My public number.

I wrote a book: "Swift language Combat Promotion" http://item.jd.com/11641501.html

Swift language Combat Promotion-9th chapter game-Running cool panda-5-6 pedaling platform how to refine

Related Article

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.