【问题标题】:why isnt my second view loading after transition?为什么我的第二个视图在转换后没有加载?
【发布时间】:2015-06-10 04:33:01
【问题描述】:

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

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

下面是第二个视图的UIViewController 类中的viewDidLoad

当它击中时

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 */
    }
}

【问题讨论】:

  • 您收到错误说明的原因。视图控制器的视图不是SKView。这是一个普通的UIView

标签: ios swift uiviewcontroller sprite-kit


【解决方案1】:

问题是您的视图控制器视图是您日常运行的磨机 UIView 而不是 SKView。假设您正在使用情节提要,单击视图控制器,然后单击视图并将类更改为 SKView 而不是 UIView。

希望对您有所帮助。

【讨论】:

  • 感谢您提供易于理解的解决方案,但是在类下拉列表中没有 SKView 选项。我还需要做点别的吗?
  • 是的,这是 Xcode 的一个奇怪错误,如果你只是输入它应该对你有用。
【解决方案2】:

我不确定您要归档什么,您可以尝试将您的超类从 UIViewController 更改为 SpriteViewController,或者您可以创建一个新的 SKView 并附加到您的视图中。

您不能将 UIView 向下转换为 SKView,因为 SKView 继承自 UIView,正如您在 apple documentation 中看到的那样

Inherits From
  NSObject
    UIResponder
      UIView
        SKView

【讨论】:

  • 我想指出我是新手,大约两周前我才开始自学,无论如何,我在我的第一个 ViewController 中设置了几乎一个 Identicle,它工作正常,我要做的就是将场景重置为我创建的另一个 SKScene 类
  • 你也可以发布你的第一堂课吗?所以我们可以试着看看发生了什么
  • 这是上面刚刚发布的新精灵套件游戏附带的标准
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2011-09-26
  • 2018-10-24
  • 2020-07-31
  • 1970-01-01
  • 1970-01-01
  • 2017-03-24
  • 1970-01-01
相关资源
最近更新 更多