如何更改Swift中的标签文本?

时间:2014-08-22 20:34:17

标签: swift

我是Swift的新手,但我不知道为什么这不起作用,我正在尝试更改此标签的文字。

import SpriteKit
var counter:Int = 0
class GameScene: SKScene {
    override func didMoveToView(view: SKView) {
        /* Setup your scene here */
        var myLabel = SKLabelNode(fontNamed:"Tahoma")
        myLabel.fontSize = 65;
        myLabel.name = "Button"
        myLabel.position = CGPoint(x:CGRectGetMidX(self.frame), y:CGRectGetMidY(self.frame));
        myLabel.text = String(counter);
        self.addChild(myLabel)
    }

    override func touchesBegan(touches: NSSet, withEvent event: UIEvent) {
        /* Called when a touch begins */

        for touch: AnyObject in touches {
            counter++
            var lol = self.childNodeWithName("Button").text=counter
        }
    }

    override func update(currentTime: CFTimeInterval) {
        /* Called before each frame is rendered */
    }
}

1 个答案:

答案 0 :(得分:4)

你必须type casting

if let label = self.childNodeWithName("Button") as? SKLabelNode {
   label.text = counter
}