Naveen Saini
Naveen Saini

Reputation: 81

How do I pass a value from array for every increment of score

I'm making a game in SpriteKit and I have a variable score which is NSInteger() and passed which is SKLabelNode

My score score increases for every hurdle the user passes, how can I send a value from array for every increment the score makes.

This is the array, and it will have 42 items

var passed2 = ["English", "Maths", "Physics", "Chemistry"]

I'm doing it in a very lengthy way, like this:

if score==1{
   passed.text="English"
}

else if score==2{
   passed.text="Maths"
}

How can I pass the value from array one at a time with every score increment.

Upvotes: 1

Views: 126

Answers (2)

Anters Bear
Anters Bear

Reputation: 1956

Don't name your array and your label the same thing it's super confusing. I hope this is helpful.

passed.text=passed2[score] // value in array passed at index score

Reset when out of index (when score more than indexes in array).

if (score == passed2.count) {
    score = 0
}

Upvotes: 1

wonderlearner
wonderlearner

Reputation: 90

see if this helps

if(currentScore != score) {
    passed.text = passed[score-1];
}
    // where currentScore variable maintains score from earlier iteration

Upvotes: 0

Related Questions