swift – Spritekit-场景转换延迟
我有一个主屏幕,有一个按钮,当按下这个按钮时,它应该立即转换到另一个场景,但事实并非如此.它实际上需要几秒钟.有没有办法可以预先加载该场景中的所有节点? (示例:在游戏加载屏幕中)
这是我的代码: let pressButton = SKAction.setTexture(SKTexture(imageNamed: "playButtonP.png")) let buttonPressed = SKAction.waitForDuration(0.15) let buttonNormal = SKAction.setTexture(SKTexture(imageNamed: "playButton.png")) let gameTrans = SKAction.runBlock(){ let doors = SKTransition.doorsOpenHorizontalWithDuration(0) let levelerScene = LevelerScene(fileNamed: "LevelerScene") self.view?.presentScene(levelerScene,transition: doors) } playButton.runAction(SKAction.sequence([pressButton,buttonPressed,buttonNormal,gameTrans])) 解决方法
您可以在呈现场景之前预先加载您在LevelerScene中使用的SKTextures.然后,一旦加载完成,您将呈现场景.以下是Apple的文档中的一个示例,转换为Swift:
SKTexture.preloadTextures(arrayOfYourTextures) { if let scene = GameScene(fileNamed: "GameScene") { let skView = self.view as! SKView skView.presentScene(scene) } } 在您的情况下,您有几个选择: 1. class LevelerScene : SKScene { // You need to keep a strong reference to your textures to keep // them in memory after they've been loaded. let textures = [SKTexture(imageNamed: "Tex1"),SKTexture(imageNamed: "Tex1")] // You could now reference the texture you want using the array. //... } 现在在GameScene中,当用户按下按钮时: if let view = self.view { let leveler = LevelerScene(fileNamed: "LevelerScene") SKTexture.preloadTextures(leveler.textures) { // Done loading! view.presentScene(leveler) } } 没有办法让你不得不等待一点,但采用这种方法主线程不会被阻止,你可以在LevelerScene加载时与GameScene进行交互. 您也可以使用此方法为LevelerScene加载SKScene. GameScene将带您进入加载场景,这将加载纹理,然后在完成后将您移动到LevelerScene. 重要的是要注意,因为对纹理的引用是在LevelerScene中,一旦LevelerScene被取消,纹理将从内存中删除.因此,如果你想回到LevelerScene,你需要再次加载纹理. 2.在任何SKScenes出现之前,您可以在GameViewController中使用SKTexture.preloadTextures.您需要对这些纹理(可能是单个)进行强有力的引用,然后您可以在LevelerScene中引用(或者在应用程序中需要它们的任何其他地方). 使用此方法,因为SKTextures存储在场景之外,所以当您转换到下一个场景时,它们不会从内存中删除.这意味着如果您离开然后返回场景,则不必再次加载纹理.但是,如果你有很多纹理占用大量内存,你可能会遇到一些内存问题. 有关更多信息,请参阅从Working with Sprites预加载纹理到内存. 希望有所帮助! (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |