无法更改SKView backgroundColor

时间:2016-06-04 22:14:35

标签: ios cocoa-touch sprite-kit

这适用于iOS 9.我无法设置SKView的背景颜色,它始终使用默认灰色进行渲染。有办法解决这个问题吗?

let frame = CGRect(x: 0, y: 0, width: 200, height: 100)
let spriteView = SKView(frame: frame)
spriteView.backgroundColor = .blueColor()
self.view.addSubview(spriteView)

运行上述代码时,SKView为灰色而不是蓝色。我真正想要做的是设置allowsTransparency = true,但如果我无法将背景颜色更改为clearColor,我就无法工作。

其他人遇到这个?任何解决方法?

更新

即使有@Danilo的建议,这仍然显示为灰色:

let frame = CGRect(x: 0, y: 0, width: 200, height: 100)
let spriteView = SKView(frame: frame)
spriteView.backgroundColor = .clearColor()
spriteView.allowsTransparency = true
spriteView.opaque = false

更新

显然设置SKView的backgroundColor无效,但如果您将其设置为任何内容,则allowsTransparency = true不起作用。

2 个答案:

答案 0 :(得分:4)

除了添加SKView之外,我们还需要一个由SKScene呈现的SKView;然后,我们调整/更改backgroundColor的{​​{1}}。反过来,节点将添加一个节点。

例如:

SKScene

答案 1 :(得分:0)

    let spriteView = SKView(frame: CGRect(x: 0, y: 0, width: 100, height: 100))
    let spriteKitScene = SKScene(size: spriteView.frame.size)
    spriteKitScene.backgroundColor = SKColor.blackColor()
    spriteView.presentScene(spriteKitScene)
    self.view.addSubview(spriteView)