updating an SKLabel to show the right integer

独自空忆成欢 提交于 2019-12-11 06:39:39

问题


ok so first off I have an achievement Variable that is defined from a struct like so:

struct Achieve {

var node: SKSpriteNode?
var aName:String = ""
var aDes:String = ""
var aImage: String = "" {
    didSet {
        node?.texture = SKTexture(imageNamed: aImage)
    }
}
var aAmount:Int = 0
var aRequired:Int = 0
var aStage:Int = 0

}

var Ach1 = Achieve(node: nil, aName: "Player", aDes: "Games Played", aImage: "locked", aAmount: 0, aRequired: 10, aStage: 1)

My problem is when I try and change the number of the aAmount property it isn't displayed on the SKLabel that displays the amount it just stays at 0 my sklabel is defined in a function like below:

func generateLabels(location: CGPoint, page:SKSpriteNode, text: String) -> SKLabelNode {

        let node = SKLabelNode()
        node.fontColor = UIColor.whiteColor()
        node.fontName = "Helvetica"
        node.position = location
        node.fontSize = 15
        node.text = text
        page.addChild(node)
        return node
}

func menu() {

_ = generateLabels(CGPointMake(0, -285), page:page1ScrollView, text: "\(Ach1.aAmount) / \(Ach1.aRequired)") 

}

It seems to work if I make changes to aAmount when I stop running it and then change it and then run the game again. it also seems to make changes to the aAmount property during gameplay but it doesn't seem to update the label for some reason during the gameplay. Can someone please tell me why it won't update?

Also i'm updating the aAmount property like so:

 Ach1.aAmount += 1
 print("\(Ach1.Amount)")

回答1:


In your Achieve struct, add a property called label:

var label: SKLabelNode?

And change the aAmount property to look something like this:

var aAmount: Int = 0 {
    didSet {
        label?.text = "\(aAmount) / \(aRequired)"
    }
}

Now when you generate the labels, replace this:

_ = generateLabels(CGPointMake(0, -285), page:page1ScrollView, 
    text: "\(Ach1.aAmount) / \(Ach1.aRequired)")

with this

var label = _ = generateLabels(CGPointMake(0, -285), page:page1ScrollView, 
    text: "\(Ach1.aAmount) / \(Ach1.aRequired)")
Ach1.label = label

Now when you change the aAmount property of Ach1, the label's text should change as well.

Suggestions:

Judging from this and your last question about sprite node images not updating, I think your programming skills are not mature enough. In the long term, you should learn the basics first. Try playing around with UIKit and read more others' code. See how other people do things. You will definitely learn a lot. In the short term though, you shouldn't make each of all these properties binds to a node. That will make Achieve too dependent. What you should do is make Achieve a subclass of SKSpriteNode and add those other sprite nodes and label nodes as sub nodes of the Achieve node.




回答2:


If I read your code, there isn't any part where you update your SKLabelNode.

If you call the function :

func menu() {
   _ = generateLabels(CGPointMake(0, -285), page:page1ScrollView, text: "\(Ach1.aAmount) / \(Ach1.aRequired)") 
}

for example to didMoveToView, it generate a label with your aAmount value when you call your scene (so 1 time only as didMoveToView works).

Instead, assuming that you call this menu function many times, it generate everytime a new label but there is no code written from you where you remove the previous label or labels added in past (see page.addChild(node)) and I don't understand why you want to return a label from your function if you don't use it, in fact you assign your result to _. Also you don't explain what is "page1ScrollView", are you using a scrollview?

Anyway, the best mode to update your label is simply to create 1 time only (for example on didMoveToView) and, everytime you want to update it, doing:

myNodeLabel.text = "\(Ach1.aAmount) / \(Ach1.aRequired)"

instead of re-create it everytime. I hope this helps to understand the dynamics that prevent your label to upgrade.




回答3:


You have to reassign the text of the SKLabel. So when you call the function to change the number (or whatever it is), also run the code to make the label what you want it to be. My guess as to why it only works sometimes is that sometimes you call it in the update (or somewhere else that gets called repeatedly) and sometimes you call it somewhere like didMoveToView (only called once, so label doesn't get updated because of the way your code is).

Just whenever you update aAmount, add this code after it:

label.text = "\(aAmount)"


来源:https://stackoverflow.com/questions/39176379/updating-an-sklabel-to-show-the-right-integer

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!