如何设置场景的背景色?

时间:2022-02-13 06:57:00

I have put

我把

super.viewDidLoad()
self.view.backgroundColor = UIColor.yellowColor()

in

override func viewDidLoad() { 

}

of GameViewController.swift. But the background doesn't change to yellow. Then, I even add that code to:

GameViewController.swift。但是背景不会变成黄色。然后,我甚至将代码添加到:

override func didMoveToView(view: SKView) {

}

But still, the background doesn't change.

但是,背景并没有改变。

Any way can I change the color of my scene's background ?

我可以改变场景背景的颜色吗?

2 个解决方案

#1


1  

You need to set the background color of the scene not background color of your view controller's view:

您需要设置场景的背景色,而不是视图控制器视图的背景色:

// let's create a test Scene

public func setupScene() -> SKScene {
    let scene = SKScene(size: CGSize(width: 1024, height: 768))
    scene.scaleMode = .AspectFit    // define the scaleMode for this scene
    scene.backgroundColor = SKColor.lightGrayColor()   // HERE: background color

    return scene
}

EDIT: To set a "background image", you just add a SKSpriteNode to the scene

编辑:设置一个“背景图像”,你只需在场景中添加一个SKSpriteNode。

// let's create a test Scene

public func setupScene() -> SKScene {
    let scene = SKScene(size: CGSize(width: 1024, height: 768))
    scene.scaleMode = .AspectFit    // define the scaleMode for this scene

    let n = SKSpriteNode(imageNamed: "background.png")
    scene.addChild(n)
    return scene
}

#2


0  

You need to change the color of scene and not your ViewController

你需要改变场景的颜色,而不是你的视图控制器

I change my background to red

我把背景换成红色

override func didMoveToView(view: SKView) {

backgroundColor = SKColor.redColor()

}

#1


1  

You need to set the background color of the scene not background color of your view controller's view:

您需要设置场景的背景色,而不是视图控制器视图的背景色:

// let's create a test Scene

public func setupScene() -> SKScene {
    let scene = SKScene(size: CGSize(width: 1024, height: 768))
    scene.scaleMode = .AspectFit    // define the scaleMode for this scene
    scene.backgroundColor = SKColor.lightGrayColor()   // HERE: background color

    return scene
}

EDIT: To set a "background image", you just add a SKSpriteNode to the scene

编辑:设置一个“背景图像”,你只需在场景中添加一个SKSpriteNode。

// let's create a test Scene

public func setupScene() -> SKScene {
    let scene = SKScene(size: CGSize(width: 1024, height: 768))
    scene.scaleMode = .AspectFit    // define the scaleMode for this scene

    let n = SKSpriteNode(imageNamed: "background.png")
    scene.addChild(n)
    return scene
}

#2


0  

You need to change the color of scene and not your ViewController

你需要改变场景的颜色,而不是你的视图控制器

I change my background to red

我把背景换成红色

override func didMoveToView(view: SKView) {

backgroundColor = SKColor.redColor()

}