1

Goal: A red rectangle (of class Brick) should appear in a blue background with node count and fps.

Reality: Gray background and node count and fps (e.g. no red rectangle or blue background)

I have the following code:

GameViewController

import SpriteKit

class GameViewController: UIViewController {

    override func loadView() {
        self.view = SKView(frame: UIScreen.mainScreen().bounds)
    }

    override func viewDidLoad() {
        super.viewDidLoad()

        let scene: GameScene = GameScene()

        let viewAsSKView = self.view as! SKView
        viewAsSKView.showsFPS = true
        viewAsSKView.showsNodeCount = true
        viewAsSKView.backgroundColor = UIColor.blueColor()

        viewAsSKView.presentScene(scene)
    }
}

My best guess it that something's wrong with GameViewController, but I'm not sure what. viewDidLoad() must've been called because I get the fps and node count.

GameScene

class GameScene: SKScene {

    override func didMoveToView(view: SKView) {

        let myBrick = Brick()
        myBrick.position = CGPointMake(100, 100)
        self.addChild(myBrick)

    }

}

By the lack of complexity, I assume nothing is wrong with GameScene.

AppDelegate

import UIKit

@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {

    var window: UIWindow?


    func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
        self.window = UIWindow(frame: UIScreen.mainScreen().bounds)
        self.window!.backgroundColor = UIColor.orangeColor()       
        self.window!.rootViewController = GameViewController()
        self.window!.makeKeyAndVisible()

        return true
    }

I know that nothing is wrong with my AppDelegate, because if I changed self.window!.rootViewController to UIViewController, I get my orange screen.

Fine Man
  • 455
  • 4
  • 17

1 Answers1

4

Your scene has no size. You can set a size programmatically by initializing the scene using the size initializer.

let scene: GameScene = GameScene(size:view.bounds.size)

or set your scene's scale mode to ResizeFill so that the size of your scene will always resize to match the SKView's size:

scene.scaleMode = SKSceneScaleMode.ResizeFill

Additionally you may want to view the answer here to learn more about Sprite Kit scaling.

Community
  • 1
  • 1
Epic Byte
  • 33,840
  • 12
  • 45
  • 93
  • Haha, thanks for coming to the rescue again, @Epic Byte. Unfortunately, though, your solution didn't help me out (although it probably fixed another problem that's not identifiable yet). I still got the same results. – Fine Man Jul 14 '15 at 01:49
  • @SirJony set the background color of your scene, not the SKView. – Epic Byte Jul 14 '15 at 01:50
  • @SirJony I'm guessing there is an error displaying your Brick class. – Epic Byte Jul 14 '15 at 01:51
  • You're right. So my scene is up and running, but my Brick isn't. I'll take a look at the code, and post a question if I can't solve it. Thanks again! For the viewers of the future, can you update your answer with what you've shown in your comments? – Fine Man Jul 14 '15 at 02:01