我正在制作游戏的教程部分。 单击指令按钮后,屏幕将从SKScene移至UIViewController以获取教程。 问题是我的SKScene在教程之前工作正常,但是当从教程回来时,无法点击节点。 这是从SKScene转到教程的代码:
let a = (self.view?.window?.rootViewController)! as! GameViewController self.willMoveFromView(self.view!) self.view?.removeFromSuperview() a.showInstruction()GameViewController中的方法showInstruction:
let storyBoard : UIStoryboard = UIStoryboard(name: "Main", bundle:nil) let tut = storyBoard.instantiateViewControllerWithIdentifier("pvcTutorial") as! Tutorial tut.view.frame = CGRectMake(0, 0, self.view.frame.width, self.view.frame.height) addChildViewController(tut) view.addSubview(tut.view) tut.didMoveToParentViewController(self)以下是从教程回到SKScene代码:
self.parentViewController?.view.removeFromSuperview() self.parentViewController?.removeFromParentViewController() self.parentViewController?.willMoveToParentViewController(nil)我使用parentViewController的原因是因为我使用UIPageViewControl作为教程。 代码将删除UIPageViewController以返回UIPageViewController 。 我如何使用SKScene的detect节点
var loc = tap.locationInView(tap.view) loc = self.convertPointFromView(loc) let btn = self.nodeAtPoint(loc) if btn is SKLabelNode{ if btn.name != nil{ if unlockLevels.contains(btn.name!){ let gameScene : GameScene = GameScene(size: size, difficulty: btn.name!) let reveal = SKTransition.crossFadeWithDuration(NSTimeInterval(2)) self.view?.presentScene(gameScene, transition: reveal) }else{ if btn.name == "instruction"{ let a = (self.view?.window?.rootViewController)! as! GameViewController self.willMoveFromView(self.view!) self.view?.removeFromSuperview() a.showInstruction() } } } }I am working on a tutorial part of a game. When instruction button clicked, screen will move from SKScene to UIViewController for a tutorial. The problem is my SKScene works fine before tutorial, however when come back from the tutorial, the node can't be clicked. This is the code to move to tutorial from SKScene:
let a = (self.view?.window?.rootViewController)! as! GameViewController self.willMoveFromView(self.view!) self.view?.removeFromSuperview() a.showInstruction()Method showInstruction in GameViewController:
let storyBoard : UIStoryboard = UIStoryboard(name: "Main", bundle:nil) let tut = storyBoard.instantiateViewControllerWithIdentifier("pvcTutorial") as! Tutorial tut.view.frame = CGRectMake(0, 0, self.view.frame.width, self.view.frame.height) addChildViewController(tut) view.addSubview(tut.view) tut.didMoveToParentViewController(self)Here is code to move back to SKScene from tutorial:
self.parentViewController?.view.removeFromSuperview() self.parentViewController?.removeFromParentViewController() self.parentViewController?.willMoveToParentViewController(nil)The reason I am using parentViewController is because I use UIPageViewControl for tutorial. The code will remove the UIPageViewController to get back to GameViewController. How I use detect node from SKScene
var loc = tap.locationInView(tap.view) loc = self.convertPointFromView(loc) let btn = self.nodeAtPoint(loc) if btn is SKLabelNode{ if btn.name != nil{ if unlockLevels.contains(btn.name!){ let gameScene : GameScene = GameScene(size: size, difficulty: btn.name!) let reveal = SKTransition.crossFadeWithDuration(NSTimeInterval(2)) self.view?.presentScene(gameScene, transition: reveal) }else{ if btn.name == "instruction"{ let a = (self.view?.window?.rootViewController)! as! GameViewController self.willMoveFromView(self.view!) self.view?.removeFromSuperview() a.showInstruction() } } } }最满意答案
我做的解决方案很简单,而不是:
let a = (self.view?.window?.rootViewController)! as! GameViewController self.willMoveFromView(self.view!) self.view?.removeFromSuperview() a.showInstruction()我改成了:
let a = (self.view?.window?.rootViewController)! as! GameViewController a.showInstruction()它奏效了! 据我所知,在我之前的代码中,我写了从SKScene删除了SKScene 。 但是,我仍然感到困惑的是,为什么场景仍然能够显示没有发生但只是无法触及它上面的任何节点。 无论如何,问题得到解决,任何解释都会受到赞赏。
The solution I made is just simple that, instead of:
let a = (self.view?.window?.rootViewController)! as! GameViewController self.willMoveFromView(self.view!) self.view?.removeFromSuperview() a.showInstruction()I changed it to:
let a = (self.view?.window?.rootViewController)! as! GameViewController a.showInstruction()And it worked! As far as I understand is that in my previous code I wrote made SKScene removed from the GameViewController. However, I am still confused that why the scene was still able to show as nothing happened but just unable to touch any node on it. Anyways, the problem fixed and any explanation would be appreciated.
更多推荐
发布评论