It's time to get Schwifty in here! (Timers)

My cat chewed on my charing cable and destroyed it........... Therefore I was gone for a while. Also it was Christmas! :) Hope you all had an amazing Christmas and if you don't celebrate, hope you had and still have an awesome holiday!

Put your hands on the keyboard

Follow theese steps

It's about to get schwifty in here

I'm mr Noobgamemaker and today I will about how to make a Timer in Swift (the programming language that Apple uses, not Taylor Swift).

The Timer() can be used in many situations, but today we'll be using it as a countdown to end a game. :) Like the Get Schwifty song made the giant heads dissapear in the Rick And Morty episode.


So let's get staaaaaarted! :)





Step one: Make two variables and declare one as a Timer() and the other one as SKSpriteNode() in the startGame function which we made last time:)


If you haven't read the last post where I explained the basics about declaring variables I highly recommend reading that post first. :)

The first thing we have to do is create a countDown which is the timer it self, than a label shows the numbers and then an Int which counts down the label. This may sound confusing now, but I will try to explain how it works.
 
Step dos: setting up the countDownLabel :)

We do the same thing with this scoreLabelNode as we did in the last, the only different thing about this is the countdown.text is set to the value of (in this case) 5.



you can also change the font size by for example countdown.fontSize = 50





the position of the x and y of the label can also be modified. Just experiment! :)
You also have to set timer to a value, since the timer which is an int has a default value of 0


Step 3: make a count down functions.


here is where the fun begins :)

These two functions will make the countdown work. In the first function we specify the countDown we declared as a Timer(). The scheduledTimer tells the countDown declared as a Timer what to do. :)


timeInterval:
specify's how fast the countdown is in seconds.

Target:
what it's targeted toooo? Don't really know... #noob (just use self for starters and then experiment)

Selector:
in this case what function in GameScene it's connected to

UserInfo:
what the user info is? Don't really know... #noob (just use nil for starters and then experiment)

Repeats:
If you want to repeat the countdown process.

since this code #selector(GameScene.updateCounter) belongs to Objective C (the main language Apple made before Swift) we have to tell that the function updateCounter() belongs to objective C. Therefore we use @objc in front of func.

The updateCounter is connected to the countdownTimer function and makes the timer go down by 1.

We then say that the text of the countDownLabel is equal to timer, but since the countDownLabel is a SKSLabelNode and timer is an Int.

We need to modify the countDownLabel so that the timer becomes a String. This is done by encapsulating like this "\(timer)".

Now we just write in the StartGame countDownTimer() and it should work :) :)

Since this post is soo long I will not show how to end the game that we are making, but in the next one (maybe).

I want you all to experiment since learning-by-doing is soooooooooooooooooo important! Yes. Feel free to read my blog and copy the code if you, but also experiment with it as well! It's fun to try new things and motivating! :)

Hope you enjoy reading my posts :)

Feel free to follow me on Instagram and Twitter @noobgamemaker

✌✌✌out :)





Kommentarer

Populære innlegg fra denne bloggen

let's start cookin' (making a game for iPhone)

programming tips with meeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee! :D