更新SKLabel以显示正确的整数 [英] updating an SKLabel to show the right integer

查看:150
本文介绍了更新SKLabel以显示正确的整数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

好吧,首先我有一个成就变量,它是从像这样的结构中定义的:

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)

我的问题是,当我尝试更改aAmount属性的数量时,它未显示在SKLabel上,而该数量仅保持为0,我的sklabel在如下函数中定义:

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)") 

}

当我停止运行然后更改它然后再次运行游戏时,如果我对aAmount进行了更改,这似乎是可行的.它也似乎在游戏过程中对aAmount属性进行了更改,但是由于游戏过程中的某种原因,它似乎没有更新标签.有人可以告诉我为什么它不会更新吗?

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?

我还要像这样更新aAmount属性:

Also i'm updating the aAmount property like so:

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

推荐答案

在您的Achieve结构中,添加一个名为label的属性:

In your Achieve struct, add a property called label:

var label: SKLabelNode?

并更改aAmount属性,使其看起来像这样:

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)")

与此

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

现在,当您更改Ach1aAmount属性时,标签的文本也应更改.

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

建议:

从这个以及您关于子节点图像未更新的最后一个问题来看,我认为您的编程技能还不够成熟.从长远来看,您应该首先学习基础知识.尝试使用UIKit并阅读更多其他人的代码.看看其他人是如何做的.您一定会学到很多东西.不过,从短期来看,您不应该将所有这些属性中的每一个都绑定到一个节点.这将使Achieve过于依赖.您应该做的是使Achieve成为SKSpriteNode的子类,并将那些其他sprite节点和标签节点添加为Achieve节点的子节点.

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.

这篇关于更新SKLabel以显示正确的整数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆