3

I'm new to Swift and Xcode and am just playing around with SpriteKit. What I did is that I created a UIView in main.storyboard. The UIView (named overlayedGameScene) is only taking up about half of the screen, so you should be able to see the main UIView (self.view).

import UIKit
import SpriteKit
import GameplayKit

class GameViewController: UIViewController {
    @IBOutlet var overlayedGameScene: SKView!

    override func viewDidLoad() {
        super.viewDidLoad()

        self.view.backgroundColor = UIColor.blue
        self.view.alpha = 1

        if let theView = overlayedGameScene as SKView? {
            if let theScene = SKScene(fileNamed: "GameScene.sks") {
                theScene.scaleMode = .aspectFill

                theView.presentScene(theScene)
            }
        }
    }
}

It worked fine, but the only problem was that when I ran it in the simulator, the main view was grey when I had changed it to blue both in the storyboard and programmatically. I also changed the alpha both in the storyboard and programmatically.

This is what it looks like in the simulator.

Should I just add another blue UIView over the main view? Is there a reason for why this is happening.

Whirlwind
  • 14,286
  • 11
  • 68
  • 157
K King
  • 71
  • 4
  • For some reason, adding "import SpriteKit" anywhere in your project will grey out your initial view controller. Go figure. Try it. – user3113182 Apr 28 '18 at 17:14

1 Answers1

2

Note the view of your GameViewController:

enter image description here

It is by default an SKView:

enter image description here

Change the class of it to UIView and you will be good. By the way, setting the backgroundColor on an SKView has no effect.

Whirlwind
  • 14,286
  • 11
  • 68
  • 157
  • Thanks that helped so much! That was the small bit of advice I was looking for. – K King Jul 11 '17 at 09:11
  • @KKing No problem :) If this answer helped you with your issue, please consider accepting it by clicking on a checkmark in the upper left corner. – Whirlwind Jul 11 '17 at 09:19
  • `backgroundColor on an SKView has no effect`, this is not 100% true, you can set the `alpha` to < 1 with the `allowsTransparency` flag set to not render the blackness of the `SKView`. This will allow you to use a parent view for a static graphic (Works great when you want to use scaleMode `.aspectFill`, but want a high res background) – Knight0fDragon Jul 11 '17 at 12:37
  • @Knight0fDragon this is true, but is out of scope for the question – DR Haus Jul 11 '17 at 14:08
  • @DRHaus I 100% agree, just leaving a note for anyone who sees this in the future thinking it would have no effect – Knight0fDragon Jul 11 '17 at 14:10
  • @Knight0fDragon But it doesn't have an effect for what you said... Because no matter what color (`backgroundColor`) you set, the result will be the same. Maybe I misunderstood what you meant though... – Whirlwind Jul 11 '17 at 14:11
  • @Whirlwind I agree, that is why I said not 100% true, color at full opacity means nothing, it will stay black, but a color with an alpha less than 1 would cause the background color to not render at all, So the OP could set another UIView to blue if they so desire to achieve an effect they were looking for via UIKit – Knight0fDragon Jul 11 '17 at 14:17
  • @Knight0fDragon Ah okay, I get it now. – Whirlwind Jul 11 '17 at 14:18
  • @Whirlwind set `backgroundColor = clearColor` and `allowsTransparency = true` – Knight0fDragon Jul 11 '17 at 14:18