CPU usage between scenes in Swift

0

Good morning!

First forgive me if this question is very obvious, but I am still a novice in Swift and I am trying to familiarize myself with the language.

I'm trying to make a very simple game. In the main screen at the moment there are some buttons and an animated image from an array of images. Up there well. I have another button that opens a new scene without problem, and from this second scene I have another button to return to the first scene.

The problem is that if I repeat the step of going from one scene to another several times I see how the use of the CPU is triggered, and the application slows down until it is completely frozen.

I have been reading several forums in English but I have not finished learning about it. I understand that Swift leaves each scene cached to improve performance, but I do not understand why it makes such an abusive use of the CPU. I think that when you move from one scene to another you should be able to release the load of the device by removing objects from the scene in the background. Am I wrong a lot?

Can someone help me?

Sorry for the inconvenience.

EDITED: I add the code of a ViewController together with its corresponding Scene, (the other is exactly the same or very similar), and the Storyboard to try to explain to me the best possible.

ViewController.swift class GameViewController: UIViewController {

let scene = MenuScene(size:CGSize(width: 1536, height: 2048))

@IBOutlet weak var sunGIF: UIImageView!

var sunImages = [UIImage]()

var statusBarHidden = true

override func viewDidLoad() {
    super.viewDidLoad()

    let skView = self.view as! SKView

    skView.ignoresSiblingOrder = true
    scene.scaleMode = .AspectFill
    skView.presentScene(scene)

    for i in 1...45 {
        sunImages.append(UIImage(named: "\(i)")!)
    }
    for i in (1...45).reverse() {
        sunImages.append(UIImage(named: "\(i)")!)
    }
    for i in (46...89) {
        sunImages.append(UIImage(named: "\(i)")!)
    }
    for i in (46...89).reverse() {
        sunImages.append(UIImage(named: "\(i)")!)
    }

    sunGIF.animationImages = sunImages
    sunGIF.animationDuration = 4
    sunGIF.startAnimating()
}

override func prefersStatusBarHidden() -> Bool {
    return statusBarHidden;
}

/*override func viewDidDisappear(animated: Bool) { //PROBLEMAS CON EL USO DE LA MEMORIA ENTRE ESCENAS.
    super.viewDidDisappear(animated)
    scene.paused = true
}*/

}

Scene.swift class MenuScene: SKScene {

let skyBackground = SKSpriteNode(imageNamed: "skyBackground")
let cloudsBackground = SKSpriteNode(imageNamed: "cloudsBackground")
let cloudsBackground2 = SKSpriteNode(imageNamed: "cloudsBackground")
let grassBackground = SKSpriteNode(imageNamed: "grassBackground")
let grassBackground2 = SKSpriteNode(imageNamed: "grassBackground")

override func didMoveToView(view: SKView) {

    skyBackground.anchorPoint = CGPointZero
    skyBackground.position = CGPointZero
    skyBackground.zPosition = -1
    addChild(skyBackground)

    cloudsBackground.anchorPoint = CGPointZero
    cloudsBackground.position = CGPoint(x:0, y:1189)
    cloudsBackground.zPosition = 0
    addChild(cloudsBackground)

    cloudsBackground2.anchorPoint = CGPointZero
    cloudsBackground2.position = CGPointMake(cloudsBackground2.size.width-1, 1189)
    cloudsBackground.zPosition = 0
    addChild(cloudsBackground2)

    grassBackground.anchorPoint = CGPointZero
    grassBackground.position = CGPoint(x:0, y:-5)
    grassBackground.zPosition = 0
    addChild(grassBackground)

    grassBackground2.anchorPoint = CGPointZero
    grassBackground2.position = CGPointMake(grassBackground2.size.width-1, -5)
    grassBackground.zPosition = 0
    addChild(grassBackground2)

}

override func update(currentTime: NSTimeInterval) {

    cloudsBackground.position = CGPoint(x:cloudsBackground.position.x - 4, y:cloudsBackground.position.y)
    cloudsBackground2.position = CGPoint(x:cloudsBackground2.position.x - 4, y:cloudsBackground2.position.y)

    grassBackground.position = CGPoint(x:grassBackground.position.x - 1, y:grassBackground.position.y)
    grassBackground2.position = CGPoint(x:grassBackground2.position.x - 1, y:grassBackground2.position.y)

    if(cloudsBackground.position.x < -cloudsBackground.size.width) {
        cloudsBackground.position = CGPointMake(cloudsBackground2.position.x +
        cloudsBackground2.size.width, cloudsBackground.position.y)
    }

    if(cloudsBackground2.position.x < -cloudsBackground2.size.width) {
        cloudsBackground2.position = CGPointMake(cloudsBackground.position.x +
            cloudsBackground.size.width, cloudsBackground2.position.y)
    }

    if(grassBackground.position.x < -grassBackground.size.width) {
        grassBackground.position = CGPointMake(grassBackground2.position.x +
            grassBackground2.size.width, grassBackground.position.y)
    }

    if(grassBackground2.position.x < -grassBackground2.size.width) {
        grassBackground2.position = CGPointMake(grassBackground.position.x +
            grassBackground.size.width, grassBackground2.position.y)
    }

}

}

Storyboard

    
asked by Gabriel 03.04.2016 в 12:18
source

0 answers