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

It's my sixth post... Am I a pro blogger soon?...

Anyways, howdy folks!

Two post ago, read this first before going all in on this one, we became familiar to Xcode and it's wizardry.


This time we'll not become masterchefs like Paula Deen, rather we'll be cooking up a small game (programming) in Swift (No, not Taylor Swift... But Apples, no not the fruit... But the programming language that the company Apple made, that is called Swift, there!).

Okay, let's start!

step numero uno: Setting variables and SK's.

A variable called 'var' in swift is like rice or potatoes, works with everything, just remember to declare what the variable is. For example that a potato is a vegetable, but since swift isn't something you can eat we have to declare potato in a different way.

Now, if I write in gameScene.swift:

var potato = SKSpriteNode()

I declare potato as an SKSpriteNode which is a texture. 

Now that we are done declaring the variables, we then have to declare the size, position, if it's an image or just a colored square.

Step 2: Declaring what the potato is and score is.

In the func didMove(to view: SKView) if you write this, ignore the green text, these are just used for the tutorial:


The score has two parts to it. The first part is to set a variable score to an Int. An Int is a number, i'm not good at math... Sooooo yeah, anyways.
 
The other part is to set a score label which is the text that shows up on the screen:



Step 3: Let's make some noise!


Now let's add some sound to the game!

First you need to import AVFoundation where SpriteKit is imported, like this:
Then add a new variable:
var audioPlayer = AVAudioPlayer!
This allows the game to play sound, like the chilli allows you to cook spicy food. Then in a new function write this:



This takes the audio from the Assets folder and plays whenever this function is called. I added two sounds to my game. 

Step four: Let's make a simple game mechanic


in the tochesBegan function write this:

what this does is whenever you tap the potato the potato moves to a random position and the audio of your choice plays and adds the score to the scoreLabelNode (when you tap the potato the score will be the last score + 1) and airPlay + 1. The if statement says that if airPlay is equal to 10, then play airFunc (the other sound i added) and become 0 again.


I think it's enough for now. :) Next post I will show you how to add a timer, end the game and some other cool shit.

Hope you all enjoy! :D

Follow me on Instagram and Twitter @noobgamemaker
✌✌ out! :)



Kommentarer

Populære innlegg fra denne bloggen

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

programming tips with meeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee! :D