If you have successfully built the project, after running you will find it is a small plane program, if I remember correctly, this program is Spin-the-bottle:space Edition, but unfortunately, we do not need these things, we need a clean template, so, We need to clear the battlefield first and then put in some of the resources we need to use.
Open the project navigation bar, such as the icon inside the circle, or click? + 1:
Right-click Gamescene.sks, select Delete
When asked by the system, select Move to Trash
Next click Image.xcassets, then delete the spaceship picture
Trimming the Fat
Next, let's remove the useless code from the code and clean out the battlefield completely:
Start cleaning up the Gamescene.swift first:
Attention
The red part of the picture is what we want to delete, and in the following tutorial, ' -' The part of the red is the content to be deleted , the' + ' sign green part is what we want to add
Next is Gameviewcontroller.swift, where we need to delete more content.
The sights and Sounds of Swiftristhen we need to add the pictures and sound files used in the program, download the necessary pictures and sound files from here, save it to the desktop or download files or any other files, but do not put in your project folder, after decompression,
1. Drag the entire Sounds folder into the project's navigation bar and place it on top of the supporting files file, making sure to select the copy items if needed as shown:
2. Repeat the same steps and drag the Sprits.atlas folder into the navigation bar
3. Under the Images folder, select all the pictures, then open the support files in the navigation bar, drag all the selected pictures directly into it, and confirm that the copy items if needed check box must be selected
4. The final step, open the Images.xcassets file, and then select AppIcon, respectively, 3 29pt after decompression. Pictures of 40pt and 60pt are dragged to the appropriate location
So far, all the pictures and sounds and other resource files have been loaded, let's start coding!
Start at the backdeveloped iOS should be not unfamiliar scene and Viewcontroller, scene, as the name implies that we can see the screen displayed in the interface, equivalent to a stage;
In this project, Gamescene.swift will display Tetris, display the background, display the game panel, farther away, and will also be used to control the sound and so on;
Gameviewcontroller.swift, as you can understand from the controller, is a control that is equivalent to C in the MVC pattern, controlling the interaction between the user and the background logic, and so on.
It's time to write the code, and once again, I'm going to move the sentences from the original textbook:
Feel for the language in your fingers!
Statement:
The preceding plus is not really going to hit the plus sign , but to tell you that this line of code is required to be manually added to the
coordinate system
Spritekit is based on OpenGL, so its coordinate system is also native to iOS cocoa coordinates, 0,0 in Spritekit is the lower left corner. Swiftris will go from top to bottom, so our anchor point will start at the top left corner, i.e. (0,1.0). As you can see here, this is actually a percentage, the coordinates range from 0 to 1, and (0.5,0.5) is the middle of the screen.
Let and VarPopular point: Let is a constant light, equivalent to the final in Java, and Var is the equivalent of a variable. give a very clear example from StackOverflow:
The following example should be easier to understand:
can you carry it? Next we look at the Viewcontroller file:
? and! the first line in the code indicates that Gamescence is not a optional variable. What is optional? This is the variable that can be nil or not.
Swift typically enforces instantiation either in-line where you declare the variable or during the initializer, init .... In order to circumvent this requirement we ' ve added an! After the type.
The swift language uses VAR to define variables, but unlike other languages,
Swift does not automatically assign an initial value to a variable, which means that the variable does not have a default value, so it must be initialized before it is required to use the variable. If you do not initialize before using the variable, you will get an error:
for more detailed explanations, see this post directly:http://www.ruanman.net/swift/learn/4569.html
A step-by-step process to develop Tetris with Swift: No.2 preparation