[英]Swift 3: Making a Pause Menu in SpriteKit by overlaying a 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
}
}
}
附加信息:
我正在使用:
我正在测试:
有人可以解释为什么发生这种情况以及如何解决此问题。
在此先感谢您的帮助。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.