我使用XCode 7.2.1在SceneEditor中创建一个SKSpriteNode(自定义类= PlayerNode)
我发现使用方法init(编码器:NSCoder)初始化来自SKS文件的Node加载,所以这里我如何在swift中实现PlayerNode类
class PlayerNode : SKSpriteNode {
required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
self.initializePhysics()
}
func initializePhysics() {
/* blabla */
self.color = SKColor.greenColor()
self.colorBlendFactor = 1.0
self.blendMode = .Replace
}
}
当我运行项目时,节点颜色不会改变甚至一点点。 但是当我在GameScene对象的方法didMoveToView中更改颜色时,如:
class GameScene: SKScene {
var player = SKSpriteNode()
override func didMoveToView(view: SKView) {
super.didMoveToView(view)
self.player = (self.childNodeWithName("player") as? SKSpriteNode)!
self.player.color = SKColor.greenColor()
self.player.colorBlendFactor = 1.0
self.player.blendMode = .Replace
}
}
现在变色了。 有没有办法在PlayerNode类中设置颜色?
答案 0 :(得分:0)
您创建了一个SKSpriteNode:
var player = SKSpriteNode()
但你想要:
var player = PlayerNode()