let scene = GameScene(size: self.size) scene.scaleMode = .aspectFill let animation = SKTransition.fade(withDuration: 1.0) self.view?.presentScene(scene,transition: animation) self.removeAllChildren() self.removeAllActions() self.scene?.removeFromParent()
1.编辑:我意识到为什么我会收到这个警告:“我正在尝试将节点添加到已经拥有父节点的父节点”,因为我拥有类外部场景的所有变量以及全局变量.但是,现在当游戏重新开始时,游戏就在左下角.为什么会这样,我该如何解决这个问题呢? – 固定
2.Edited:现在一切正常,但现在我担心的是即使删除了每个节点并且fps不会随着时间的推移而丢失,也不会调用deinit {}.这是我在GameViewController中用于设置场景和我的GameScene中的内容(每个与场景相关的实例基本上都是相关的):
import UIKit import SpriteKit import GameplayKit var screenSize = CGSize() class GameViewController: UIViewController { override func viewDidLoad() { super.viewDidLoad() if let view = self.view as! SKView? { // Load the SKScene from 'GameScene.sks' if let scene = SKScene(fileNamed: "GameScene") { // Set the scale mode to scale to fit the window scene.scaleMode = .aspectFill screenSize = scene.size // Present the scene view.presentScene(scene) } view.ignoresSiblingOrder = true view.showsFPS = true view.showsNodeCount = true } }
然后我的GameScene基本上是:
import SpriteKit import GameplayKit class GameScene: SKScene,SKPhysicsContactDelegate { //Declare and initialise variables and enumerations here deinit{print("GameScene deinited")} override func didMove(to view: SKView) { //Setup scene and nodes } override func touchesBegan(_ touches: Set<UITouch>,with event: UIEvent?) { //Do other things depending on when and where you touch //When I want to reset the GameScene let newScene = GameScene(size: self.size) newScene.anchorPoint = CGPoint(x: 0.5,y: 0.5) newScene.scaleMode = self.scaleMode let animation = SKTransition.fade(withDuration: 1.0) self.view?.presentScene(newScene,transition: animation) }
任何答案将不胜感激:)