ios - SKLabelNode 不改变 fontColor

标签 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)

这样颜色变化才会生效。这对我来说像是一个 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/

相关文章:

ios - 在另一个类中触发一个函数

swift - WorldNode 与对象生成冲突

ios - 为什么我在 swift 混合项目成功运行时收到警告?

ios - 游戏中心排行榜已弃用?

ios - 无法对 SKPhysicsbody 施加脉冲

ios - 如何过滤在 Core Data 中有对象的 TableView ?

ios - 在变量上使用 POP 时,类没有初始值设定项

ios - Venmo支付SDK链系统

objective-c - NSCollectionView 如何滚动到所选项目

ios - 调整 subview 大小以适应 super View 框架