2016-04-24 56 views
1

我正在尝试更改我在游戏场景中添加的基本文本标签的文本。 但我不知道如何通过代码来做到这一点。 现在我为场景编辑器中的标签分配了一个名称,但是我不知道如何从这里继续并为标签标记新的文本值。如何更改游戏场景中的文本标签的文本

目前我使用

let scoreLabel = childNodeWithName("scoreLabel") as! SKLabelNode 

但我收到的实例成员的类型“childNodeWithName‘SKNode’的错误消息“使用;?你的意思是使用类型‘SKNode’,而不是 的价值,但因为我想利用文本拉贝的功能时,SKNode类是不够的我。

任何人谁得到了一些帮助吗?

+0

如果您想了解更多有关该错误的信息,请提供更多有关此代码放置位置的上下文(例如,它是场景/某个其他类的属性,这可能是错误的,或者它是通过某种方法在本地定义的等等)。 – Whirlwind

+0

@Whirlwind基本上,我只是将该行放入GameScene类中,在这里我还放置了其他变量和定义。但我正在寻找一个机会来做这样的事情:scoreLabel.text =“3” – boehmatron

+0

我把计时器的设置放在一个单独的函数中,并在“didMove”上调用它,解决了问题:)谢谢! – boehmatron

回答

0

这是如何设置SKLabelNode无论是作为一个属性的示例和本地和acce SS这些变量后面的代码:

import SpriteKit 

class GameScene: SKScene { 

    let label = SKLabelNode(fontNamed: "ArialMT") 

    override func didMoveToView(view: SKView) { 

     label.text = "This is a label defined as a property of scene"; 
     label.fontSize = 35; 
     label.fontColor = .blackColor() 
     label.position = CGPoint(x:frame.midX, y:frame.midY) 

     addChild(label) 

     let anotherLabel = SKLabelNode(fontNamed: "ArialMT") 
     anotherLabel.text = "This is a label defined localy and added to a node tree" 
     anotherLabel.name = "anotherLabel" 
     anotherLabel.fontSize = 35; 
     anotherLabel.fontColor = .whiteColor() 
     anotherLabel.position = CGPoint(x:frame.midX, y:frame.midY - 100.0) 

     addChild(anotherLabel) 

    } 


    override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) { 


     self.label.text = "Your text" 

     if let anotherLabel = childNodeWithName("anotherLabel") as? SKLabelNode { 

      anotherLabel.text = "Your text" 
     } 
    } 
} 

在财产的情况下,访问也很简单self.label.text = "..."。对于添加到节点树的局部变量,您可以使用其名称属性稍后使用childNodeWithName方法查找它。