简体   繁体   English

如何使用SpriteKit以编程方式快速更改背景颜色

[英]How do I change background colour programmatically in swift using SpriteKit

I have this game and I have a function for when each different game mode is called, I want to change the colour of the background when a mode is clicked, but when I do It nothing happens. 我有这个游戏,并且有一个用于调用每个不同游戏模式的功能,当我单击某个模式时,我想更改背景的颜色,但是当我这样做时,它什么也没发生。 Could you please tell me how to do it, Thanks 你能告诉我怎么做吗,谢谢

override func update(_ currentTime: TimeInterval) {
    // Called before each frame is rendered


    switch currentGameType {
    case .easy:
        enemy.run(SKAction.moveTo(x: ball.position.x, duration: 1.3))
        break
    case .medium:
        enemy.run(SKAction.moveTo(x: ball.position.x, duration: 1.0))
        self.view?.backgroundColor = UIColor.white //I TRY IT HERE
        break
    case .hard:
        enemy.run(SKAction.moveTo(x: ball.position.x, duration: 0.7))
        break
    case .player2:

        break
    }

Your scene's background has nothing to do with the view's background. 场景的背景与视图的背景无关。

You should set the scene's background color instead. 您应该改为设置场景的背景色。 The scene, in this case, is self : 在这种情况下,场景是self

self.backgroundColor = .white

However, if you want to use an image for the background, you need to add a new node covering the whole scene and set the texture of the node to be your image. 但是,如果要将图像用作背景,则需要添加一个覆盖整个场景的新节点,并将该节点的纹理设置为图像。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使用 Swift 5 在 SpriteKit 中以某个角度应用脉冲? - How do I applyImpulse at a certain angle in SpriteKit using Swift 5? 如何在 Fscalender 自定义 Swift 中更改背景颜色 - How to change background colour in Fscalender customize Swift 如何使用SpriteKit替换背景图片? - How do I replace a background image using SpriteKit? 如何使用Swift 2(SpriteKit)以编程方式添加对象进行查看? - How to programmatically add objects to view using Swift 2 (SpriteKit)? 如何在Swift中以编程方式更改NavigationBar的高度? - How do I programmatically change the height of a navigationBar in Swift? 如何在 swift 中更改除弹出视图外的总屏幕背景颜色 - How to change total screen background colour except popup view in swift 如何在TableView单元格swift3中更改按钮背景颜色 - how to change button background colour in tableview cell swift3 如何通过按钮开始游戏? 我正在使用Xcode 6,Swift和SpriteKit - How do I start this game with a button? I'm using Xcode 6, Swift, and SpriteKit 如何更改 UICollectionView 中所选项目的单元格背景颜色,并将其他单元格颜色更改为默认值 swift - how to change cell background colour on selected item in UICollectionView and change other Cell colour to default in swift 如何使用SpriteKit将背景图像放置得比场景更宽? - How do I place a background image wider than the scene using SpriteKit?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM