从子视图移回时,Swift convertPointFromView返回NaN(Swift convertPointFromView returns NaN when move back from c

编程入门 行业动态 更新时间:2024-10-17 19:34:44
从子视图移回时,Swift convertPointFromView返回NaN(Swift convertPointFromView returns NaN when move back from child view)

我正在制作游戏的教程部分。 单击指令按钮后,屏幕将从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.

更多推荐

本文发布于:2023-08-04 23:22:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1423373.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:视图   从子   Swift   移回时   convertPointFromView

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!