转换后为什么我的第二个视图加载?

时间:2015-06-10 04:33:01

标签: ios swift uiviewcontroller sprite-kit

我有两个视图,一个工作正常的初始视图,以及我从第一个视图上的按钮转换到它后尝试显示的第二个视图。

但是,当我点击按钮时,会发生转换,屏幕变为空白。

以下是第二个视图的viewDidLoad类内的UIViewController

当它击中

let sKView = view as! SKVIEW

行吐出来

  

无法将'UIView'(0x10a313eb0)类型的值转换为'SKView'(0x1094d5718)。
  (lldb)

如何让它显示视图?

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)
}

头等舱

类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)
    }
}

第二个场景

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 个答案:

答案 0 :(得分:0)

我不确定您要归档的是什么,您可以尝试将您的超级班级从type MapEventType2Handler = Dictionary<ControlEventType, Dictionary<number, (sender: IControl, eventType: ControlEventType, order: ControlEventOrder, data: any) => void >>; 更改为UIViewController,或者您可以创建新的SpriteViewController并附加到您的视图中。

你不能将UIView向下转换为SKView,因为SKView继承自UIView,你可以在apple documentation

中看到
SKView

答案 1 :(得分:0)

问题是你的视图控制器视图是你的日常工厂UIView而不是SKView。假设您正在使用故事板,请单击视图控制器,然后单击视图并将类更改为SKView而不是UIView。

enter image description here

希望这有帮助。