1

I am trying to make a survival game where each level the player has to survive the game for so long in order to get to advance to the next level. However, when I go from level to level, I cannot figure out how to update the score for each scene. I understand how to add and remove subviews, but that is not the problem. The problem is calling a variable in a different scene, and changing that variable from the new scene. I will leave some code here in order for reference. Thank you in advance.

In Level1.swift,

I declare the score var:

var Score = Int()

I then add to it when I want to, and represent it as shown as a label.

Score++


ScoreLabel.text = "\(Score)"

Then in Level2.swift

I declare the variable again

var ScoreLabel = UILabel()

And add to it as well whenever needed... However, the only thing that happens is a new label comes up and overlaps the score label already shown...

Score++


ScoreLabel.text = "\(Score)"

How do I keep a running score from scene to scene in swift?

1.This is where I declare the variable, I only do this in this scene

2.This is where I declare the change of scene, I haven't yet, but this is how I plan on changing each scene

3.This is where I add the subview of the label, I do this the same way in each scene

4.This is where I add to the variable, i call upon it this way in each scene

Lozo
  • 79
  • 10
  • This should be what you're looking for http://stackoverflow.com/questions/24024549/using-a-dispatch-once-singleton-model-in-swift – 0x141E Feb 01 '16 at 07:50
  • @0x141E How do I go about declaring my global singleton as an Int? – Lozo Feb 02 '16 at 03:27

1 Answers1

0

The UILabel is not part of your scene. It's an element of the container view. Therefore it is always visible and overtime you create a new one, it will be shown on top of the existing label.

The easiest way is to use global variables. Just place

var Score = Int()
var ScoreLabel = UILabel()

outside of the class definition (and add it only once to the container view).

Another option is to use an SKLabel and add it to the scene.

This stack overflow question is about a different issue, but shows a sample how to use an SKLabel for scoring information: score label display value overwrites

Community
  • 1
  • 1
Stefan
  • 5,203
  • 8
  • 27
  • 51
  • This logic makes sense to me but what do you mean as in outside of the class definition? do you mean list it before the class definition? Also, what do you mean by add it only once to the container view? I apologize but if you could clear that up for me it would be great – Lozo Feb 01 '16 at 20:02
  • Yes. Declare it before the class definition. – Stefan Feb 01 '16 at 20:40
  • Should i declare it once only in the first level? – Lozo Feb 01 '16 at 20:41
  • Add it only once to the container view means add the UILabel only in scene 1. Don't add it for the other scenes again. – Stefan Feb 01 '16 at 20:43
  • Ah, so then do not declare it ever again in any of the other scenes? And then just refer to it in the other scenes – Lozo Feb 01 '16 at 20:44
  • Yes. Only in scene 1. Sorry I will be offline for one or two hours. I can have a look later again. – Stefan Feb 01 '16 at 20:44
  • Ok thank you, not having any problem referring to those variables but it's just not working – Lozo Feb 01 '16 at 20:55
  • Can you show a little bit more code? Especially where you are adding the Label and creating the Scenes? – Stefan Feb 01 '16 at 22:03