繁体   English   中英

SpriteKit Swift:SKView返回零

[英]SpriteKit Swift: SKView returning nil

我正在使用spritekit制作游戏。 我在游戏中使用操纵杆,该操纵杆在名为“操纵杆”的单独SKNode类中声明。

在此类中,我将UIGesturerecongiser添加到该类的视图中。 在构造方法中,参数之一是从游戏场景传递的SKView。

Joystick类中的构造方法:

 init(colour: UIColor, position: CGPoint, skView: SKView) {
    //constructor method
    self.colour = colour;
    self.parentposition = position;
    self.view = skView;

    super.init();

    //setup properties
    //user interaction is needed to allow touches to be detected
    self.isUserInteractionEnabled = true;
    //setup
    setup();
}

在游戏场景中,我像这样初始化类:

class GameScene: SKScene {

     func setupJoyStick() {
        let joystick1 = Joystick(colour: UIColor.red, position: CGPoint(x: screenwidth / 3, y: screenwidth / 10 * 1.5), skView: self.view!)
        self.addChild(joystick1)
    }
}

错误:

当我运行我的应用程序时,由于“ self.view”返回nil且由于强制展开而导致错误,因此会导致错误。

定义视图的位置:

if let scene = GKScene(fileNamed: "GameScene") {

        // Get the SKScene from the loaded GKScene
        if let sceneNode = scene.rootNode as! GameScene? {

            // Copy gameplay related content over to the scene
            sceneNode.entities = scene.entities
            sceneNode.graphs = scene.graphs

            // Set the scale mode to scale to fit the window
            sceneNode.scaleMode = .aspectFill
            sceneNode.size = view.bounds.size
            // Present the scene
            if let view = self.view as! SKView? {

                view.presentScene(sceneNode)

                view.ignoresSiblingOrder = true

                view.showsFPS = true
                view.showsNodeCount = true
            }
        }
    }

附加信息:

我正在使用:

  • Xcode 9.2
  • 斯威夫特4
  • 雪碧套件

我正在测试:

  • 苹果6s
  • 最新版本的IOS(非Beta版),最新公共发行版。

有人可以解释为什么发生这种情况以及如何解决此问题。

在此先感谢您的帮助。

看起来您正在尝试获取SKScene view属性,但它为零。 这是因为您没有介绍SKScene

不幸的是,我还没有使用SpriteKit但是您可以在这里找到有关view属性以及在这里有关SKScene 信息

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM