![]()
这是我的代码:
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.
在GameScene中预先保存一个需要在LevelerScene中使用的纹理数组:
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预加载纹理到内存.
希望有所帮助!
总结以上是内存溢出为你收集整理的swift – Spritekit-场景转换延迟全部内容,希望文章能够帮你解决swift – Spritekit-场景转换延迟所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
微信扫一扫
支付宝扫一扫
评论列表(0条)