why isnt my second view loading after transition?

144 views Asked by At

I have two views, the initial one that works fine, and a second one that I am trying to display after I transition to it from a button on the first view.

However, when I click the button, the transition happens and the screen goes blank.

Below is the viewDidLoad inside of the UIViewController class for the second view.

Also when it hits the

let sKView = view as! SKVIEW

line it spits out

Could not cast value of type 'UIView' (0x10a313eb0) to 'SKView' (0x1094d5718).
(lldb)

How do I get it to display the view?

class PlayViewController: UIViewController {

    var scene2: PlayScene!

override func viewDidLoad() {
    super.viewDidLoad()

    let skView = view as! SKView
    skView.showsFPS = true
    skView.showsNodeCount = true

    scene2 = PlayScene(size: skView.bounds.size)

    skView.ignoresSiblingOrder = true

    /* Set the scale mode to scale to fit the window */
    scene2.scaleMode = .AspectFill

    skView.presentScene(scene2)
}

first class

class GameViewController: UIViewController {

override func viewDidLoad() {
    super.viewDidLoad()

    if let scene = GameScene.unarchiveFromFile("GameScene") as? GameScene {
        // Configure the view.
        let skView = self.view as! SKView
        skView.showsFPS = true
        skView.showsNodeCount = true

        /* Sprite Kit applies additional optimizations to improve rendering performance */
        skView.ignoresSiblingOrder = true

        /* Set the scale mode to scale to fit the window */
        scene.scaleMode = .AspectFill

        skView.presentScene(scene)
    }
}

the second scene

import SpriteKit

class PlayScene: SKScene {
    override func didMoveToView(view: SKView) {
        /* Setup your scene here */
        let myLabel = SKLabelNode(fontNamed:"Chalkduster")
        myLabel.text = "SCENE 2!";
        myLabel.fontSize = 65;
        myLabel.position = CGPoint(x:CGRectGetMidX(self.frame), y:CGRectGetMidY(self.frame));

        self.addChild(myLabel)
    }

    override func touchesBegan(touches: Set<NSObject>, withEvent event: UIEvent) {
        /* Called when a touch begins */

        for touch in (touches as! Set<UITouch>) {
            let location = touch.locationInNode(self)

            let sprite = SKSpriteNode(imageNamed:"Spaceship")

            sprite.xScale = 0.5
            sprite.yScale = 0.5
            sprite.position = location

            let action = SKAction.rotateByAngle(CGFloat(M_PI), duration:1)

            sprite.runAction(SKAction.repeatActionForever(action))

            self.addChild(sprite)
        }
    }

    override func update(currentTime: CFTimeInterval) {
        /* Called before each frame is rendered */
    }
}
2

There are 2 answers

2
Skyler Lauren On BEST ANSWER

The issue is your view controllers view is your everyday run of the mill UIView instead of an SKView. Assuming you are using a storyboard click on the view controller and then click on the view and change the class to SKView instead of UIView.

enter image description here

Hopefully that helps.

3
Icaro On

I am not sure what are you trying to archive, you can try change your super class from UIViewController to SpriteViewController or you can create a new SKView and append to your view.

You cannot downcast a UIView to a SKView as a SKView inherit from UIView as you can see in the apple documentation

Inherits From
  NSObject
    UIResponder
      UIView
        SKView