Step by step, we will teach you how to use Swift to develop the Russian square: No. 4 ticking clock mechanism, swiftno.4

Source: Internet
Author: User

Step by step, we will teach you how to use Swift to develop the Russian square: No. 4 ticking clock mechanism, swiftno.4

Why is the title "ticking clock mechanism?

Presumably, we have all played tetris. What are the different shapes of things? You know, they will drop faster and faster as the level of the game increases. Yes, we also need to imitate that and make our own clock mechanism.

We can see thatSKSceneThere is a function in it.Update (currentTime: CFTimeInterval). This function is called by no frame. Frame, frame, what is it? You can understand that a frame is a static image. If many images are played continuously for a short period of time, they become an animation. When your eyes start to predict each frame of image, your game will look a little slow. Let's look at the GIF image below, do you think that the ball does not look the same as the following ball block when it is 60 FPS?

I specifically checked what is motion blur, which is actually the same as the slow shutter in photography.


OK. After the frame is introduced, let's modify the update function in GameScene. modify it as shown in the following figure:



Okay. Let's continue to explain this update in segments.

#1

First, we define a constant,TickLengthlevelOneThis constant will be used to display our slowest game speed, because the longer the interval between each image, the slower it looks, right?

Set it600Millisecond, that is, every 0.6 seconds, our shape will drop down a row.

#2

We have defined some new variables.

TickLengthMillisAndLastTickIt's easy. We have already met this definition method. The first constant that we have previously defined is assigned.TickLenghtLevelOneThe second is used to indicate the time of the last recordNSDateType.

But, you knowTick :( ()-> ())?What is it? It looks really weird.

Tick is actually calledClosure (closure)If you are familiar with object-c, the closure is the block in it ).

We have already touched on the function definition method before, and we know the meaning of the symbol. In tick, ()-> ())? This closure does not require any parameters or returns anything. Because it is optional, it may be nil.

At the beginning, I was puzzled by this ()-> (). It didn't need any parameters, and it returned noting. What did it do? Later, I finally understood the meaning of Stackoverflow's reply from a great god.


Yes, so far, we have tick. You can call it a variable or a pointer to a function. We call it closure. So far, this closure has no significance. It is a placeholder and tells you that there is something here. Of course, in the middle of the project, our tick closure will be useful.

So don't worry. You can think that the closure here is a placeholder, for example, I first occupied the building. When I want to say something, I am talking about it, this time, I think it's not good. I can change it, that's the nb.


For more official closure documents, click here


I also recommend another Chinese Translation


More clear and expressive articles on closures


You can see more amazing features there

#3

Let's continue to look at it. We will introduce a new variable.

If lastTick is nil, we are currently in a paused state, so we just need to return directly without any action. But if not, it indicates that the game is currently in progress, we have to do something.

TimePassedThis variable can be seen from its name as a variable that represents how long has elapsed, how long has elapsed, and when can it be used as a reference? Compare lastTick and now, because lastTick records the time point of the last record, it is definitely regressing from now on, so,TimeIntervalSinceNowThis function returns the difference from the current time. Because it is the past time point, all this value must be negative, and the Unit is millisecond, so we need *-1000 to convert it into a positive number and the unit is the number of seconds, because our tickLengthMillis is only 0.6 seconds.

That is to say, if the interval between the current time and the last record exceeds 0.6 seconds, we will execute the following action:

Record the current time, and then tick it;

At this stage, tick is really empty, so nothing will happen, but you need to know that it can happen something, you only need to assign a function to tick.

Because we use lastTick. timeIntervalSinceNow, learn more about the things that interest swift.DotSyntax


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.