Why is the headline called "Ticking clock mechanism"?
Presumably we have all played Tetris, those different shapes of things, which are the different shapes, you know, will increase with the level of the game to decline more and more quickly. Yes, we have to imitate that and make our own clock mechanism.
We can see that there is a function update (CURRENTTIME:CFTIMEINTERVAL)inside the skscene . This function is called without a frame. Frames, frame, what is it? You can understand that a frame is a static picture, if a lot of pictures in a short period of time to play continuously, it becomes an animation. When your eyes start to predict each frame of the image, your game will look a bit slow, we look at the following GIF picture, you will think that the 60fps when the ball looks like the following ball block?
I checked it out. What is called motion blur is actually the same slow shutter in photography.
OK introduce the frame, let us modify the update function in our Gamescene, as shown in the following diagram to modify it:
OK, let's go on to the next section to explain this update.
#1
First we define a constant,ticklengthlevelone, which will be used to show our slowest game speed, because the longer the interval between each picture, the slower it looks, right?
Let's set it to a millisecond, which means that every 0.6 seconds, our shape drops one line down.
#2
We have defined a number of new variables.
ticklengthmillis and Lasttick are simple, and we've come across this way of defining them before. The first is given a constant Ticklenghtlevelonethat we have defined before, and the second is used to represent the time we last recorded, an instance of the nsdate type.
But, but, you know tick: ((), ())? What is it, man? It does look very weird.
Tick is actually a thing called closure (closure) in Swift, and if you're familiar with object-c, then the closure is the block.
We have been exposed to the definition of function before, and know the meaning of this symbol, that in tick, ((), ())? Indicates that the closure does not require parameters, nor does it return anything. Because it is optional, it is certainly possible that it is nil.
At the beginning of the study, I have no idea of this (), it does not need any parameters, it returned to the noting, then what is it? Later did not hold on the StackOverflow asked a great God's reply let me finally understand its meaning.
Yes, here, so far, we have this tick, pointing to the function of you call it variable or, call it a pointer, we call it closure; So far, this closure no meaning, it is a placeholder, tell you here is a thing, of course, to the middle of the project, Our tick closures will be useful.
So do not worry, you can think of the closure here is a placeholder, like I first to occupy the floor, and so I want to say what I said, and this time I feel that it is not good, I can also change, so NB.
For more official documents on closure, please click here
I also recommend another Chinese translator.
Clearer and more expressive articles about closures
You can see more amazing features there.
#3
Let's take a look down and we'll introduce a new variable.
If Lasttick is nil, then we are currently in a paused state, so we just return to the good, do not need to do any action, but if not, the game is currently in progress, we have to do something.
timepassed This variable from its name can be seen to indicate how long the past a variable, how long, and when as a reference? Compare Lasttick and now, because Lasttick records the last recorded point in time, which is definitely backwards, sotimeintervalsincenow the function can return and the current time difference. Because it is the past time point, all this value must be negative, and the unit is milliseconds, so we need *-1000 to turn it into a positive and the number of seconds, because our ticklengthmillis is 0.6 seconds.
That is, if the current time and the last record are more than 0.6 seconds apart, we will perform the following actions:
Record the current time, then tick;
At this stage, because tick is a really empty thing, so nothing happens, but you know, it can happen something, as long as we give the tick a function on the line.
Because we used Lasttick.timeintervalsincenow, so interested in learning to learn more about Swift's dot syntax
For more information on DOT syntax click here
Now, we also notice that this tick notation is very fastidious,? is before (), so the wording is that we first check whether the tick exists, if it exists and then call it, if it does not exist there is no need to call, it is equivalent to the following code:
#4
Here the two functions are relatively simple, call start, because Lasttick is not nil, scene will start to refresh the screen, call stop after the update function will always return, so do not refresh the screen
Step-by-step teaching you to develop Tetris with Swift: No.4 ticking clock mechanism