SKLabelNode不会更改字体颜色

时间:2014-06-10 20:25:03

标签: ios objective-c sprite-kit swift

我的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)

以便颜色变化生效。这看起来像是对我的黑客攻击,我想知道我做错了什么或者是否还有其他方法可以做到这一点。

1 个答案:

答案 0 :(得分:0)

我发现如果我对场景执行某些操作(如空操作),精灵会更新,例如:

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

}

每次修改精灵时都会调用updateUI,例如:

MainViewController.viewController.updateUI()

不理想,因为你必须手动调用updateUI,但这是我提出的最好的:(