-1
我正在做一個精靈套件遊戲。那麼,我試圖在加載屏幕停留在屏幕上時在後臺加載精靈。我有以下的功能。它做我想要的,但「有時」它崩潰。相同的代碼,它爲什麼會崩潰或不崩潰?我在iPhone 5,iPhone 6+和iPad 2上進行了測試。該功能在iPhone 5和6+上的崩潰並不多,但在iPad 2上崩潰了很多。預先感謝。這是功能;iOS調度異步崩潰
func loadingScreen() {
//Initialize loading sprite.
let loadBg = SKSpriteNode(imageNamed: "loadBg.png");
loadBg.size = CGSize(width: self.size.width, height: self.size.height);
loadBg.position = CGPoint(x: self.size.width/2, y: self.size.height/2);
loadBg.zPosition = 10;
self.addChild(loadBg);
let loadBar = SKSpriteNode(imageNamed: "load1.png");
loadBar.size = CGSize(width: self.size.width/3, height: (self.size.width/3)*0.20);
loadBar.position = CGPoint(x: self.size.width/2, y: self.size.height*0.1);
loadBar.zPosition = 11;
self.addChild(loadBar);
var loadAni:[SKTexture] = [];
for index in 1...17 {
loadAni.append(SKTexture(imageNamed: "load\(index).png"));
}
loadBar.runAction(SKAction.repeatActionForever(SKAction.animateWithTextures(loadAni, timePerFrame: 0.01)));
let qualityOfServiceClass = QOS_CLASS_BACKGROUND
let backgroundQueue = dispatch_get_global_queue(qualityOfServiceClass, 0)
dispatch_async(backgroundQueue, {
// Codes.
self.startGame()
dispatch_async(dispatch_get_main_queue(), {() -> Void in
// Codes after loading.
self.objectSlider.runAction(SKAction.repeatActionForever(SKAction.moveByX(-self.size.width/5.8, y: 0, duration: 0.5)))
self.printPlayer()
self.isGameOver = false;
loadBg.runAction(SKAction.sequence([SKAction.fadeOutWithDuration(0.2),SKAction.removeFromParent()]))
loadBar.runAction(SKAction.sequence([SKAction.fadeOutWithDuration(0.2),SKAction.removeFromParent()]))
})
})
}
那麼基本上,如何在主線程中運行加載動畫時在後臺運行函數?那麼如何在後臺的功能完成後刪除加載動畫? – white5tone
你不能。至少不喜歡你正在嘗試這樣做。加載PNG圖像意味着壓縮數據被解碼到內存中。這是巨大的數據通脹,並佔用大量的內存。您不能只是「在後臺加載它」,因爲設備沒有足夠的內存來緩存內存中的所有圖像數據。你有一個基本的概念問題。例如,要查看與加載OpenGL紋理有關的相同問題的解決方案,請參閱此示例:http://www.modejong.com/blog/post7_load_opengl_textures_with_alpha_channel_on_ios/index.html – MoDJ
哦,我想我明白了。你能否以另一種方式爲我的遊戲製作加載屏幕? – white5tone