我在我的 SKScene 中添加了一个 SKLabelNode,它的颜色是黑色。但是在我正在做的代码中的某处
override func touchesBegan(touches: NSSet, withEvent event: UIEvent) {
for touch: AnyObject in touches {
let location = touch.locationInNode(self)
var keysArr = playerLabels.allKeys
for k in keysArr {
var playerLabel = k as SKLabelNode
if CGRectContainsPoint(playerLabel.frame, location) {
selectedPlayer = playerLabels.objectForKey(playerLabel) as AvailablePlayer
playerLabel.fontColor = UIColor.redColor()
}
}
}
}
并且它不会改变标签节点的颜色。我必须做的
playerLabel.removeFromParent()
self.addChild(playerLabel)
这样颜色变化才会生效。这对我来说像是一个 hack,我想知道我是否做错了什么,或者是否有其他方法可以做到这一点。
最佳答案
我发现如果我对场景做一些事情,比如空 Action , Sprite 就会更新,例如:
final class MainViewController: UIViewController {
/// The view controller for the main scene (self!)
private(set) static var viewController: MainViewController!
private var mainNode: SCNNode!
private var uiAction: SCNAction!
override func viewDidLoad() {
super.viewDidLoad()
...
// Retrieve and store the main node
mainNode = main.rootNode.childNodeWithName("ship", recursively: true)!
// Animate the UI (needed to allow buttons to respond - probably an iOS bug :( )
uiAction = SCNAction.repeatAction((SCNAction.rotateByX(0, y: 0, z: 0, duration: 1)), count: 1)
// Give other objects access to this view controller
MainViewController.viewController = self
}
...
/// Force the update the UI by doing a nothing animation (probably an iOS 8 bug :( )
func updateUI() {
mainNode.runAction(uiAction)
}
}
每次修改 Sprite 时,我都会调用 updateUI,例如:
MainViewController.viewController.updateUI()
不太理想,因为你必须手动调用 updateUI,但这是我想出的最好的 :(
关于ios - SKLabelNode 不改变 fontColor,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24150218/