Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

iOS Swift 2.0 Basics Swift Operators Unary Operators

Al Hughes
seal-mask
.a{fill-rule:evenodd;}techdegree
Al Hughes
iOS Development Techdegree Student 786 Points

Trouble understanding the concept of this scenario.

Hello All. I am still trying to start thinking in code and programmer speak. I can't seem to wrap my head around all of this section. Can someone give me and example of how this would work in a game situation? I understand the operators and the purpose. I guess I am having a hard time visualizing how it would work. Sorry for the long post but I'm going to try and break this down how I'm visualizing this in my head and you can tell me if this would be how it would work.

I start the game with a level score of zero

//Level One//

let levelScore = 0

var totalScore = ++levelScore

This Code would tell the computer the the starting score is zero and will increment up correct? Every point you "score" would change the total score by one. Correct?

2 Answers

Well you'd need to do something whenever you wanted to increment the score. In a game (e.g. space shoot em up) every time you detect a missile has hit an enemy you'd probably call a function that would determine how much to increment the score by, based on the type of enemy you hit, what level you were on, and anything else of interest.. something like this (which obviously if off the top of my head and very minimal, but hopefully will help illustrate the concept:

enum AlienType {
    case Defender
    case Attacker
    case Boss
}

var totalScore: Int = 0

func didHitAlien( alien: AlienType ) {
    switch alien {
    case AlienType.Defender:
        totalScore += 10
    case AlienType.Attacker:
        totalScore += 50
    case AlienType.Boss:
        totalScore += 100
    }
}

Hope this helps.

The position of the ++ operator determines the result. In your specific case after running those 2 lines totalScore and levelScore would both be 1 because placing the ++ in front of levelScore tells the computer to increment levelScore first, then assign it's value to totalScore. If the ++ was after levelScore then totalScore would be 0 and levelScore would be 1 at the completion. You should be able to see this if you create a playground and do some playing. :) Hope this helps.

Al Hughes
seal-mask
.a{fill-rule:evenodd;}techdegree
Al Hughes
iOS Development Techdegree Student 786 Points

Thank you. That clears up that portion of it. So how would determine how much each point is worth or how much to increment it by? In a game scenario would you have to right a new line of code everyone time a point is made?