1

I have this piece of code:

self.view!.presentScene(Level1(size: self.size), transition: transition)

It's called from a menu, when I click a button, it works fine but there seems to be some kind of lag.

I click the button and after a couple of seconds I see the transition and then the next view loads.

Is this normal - what causes the lag?


So after Whirlwinds comment about sound files , I removed them all, no apparent change.

I looked at the screen with CPU , Memory etc.. when I try and load the scene CPU usage shoots up to 99% , then drops down to 80% when the scene has loaded.

Memory is around 44%.

So maybe that does suggest I should preload the images?

Would do this on the menu scene - after the first white screen, from the users point of view they wouldn't notice a couple of extra seconds of loading?


Update: 25th April

Im now preloading textures like this:

SKTextureAtlas.preloadTextureAtlases([

        tilesAtlas,

        PlayerShootAtlas,
        PlayerForwardsAtlas,
        PlayerBackwardsAtlas,
        PlayerIdleAtlas,
        PlayerJumpAtlas

    ], withCompletionHandler: { () -> Void in

        print("----> Before loaded ...")

        self.loadMenu()

        print("----> After loaded ...")
    })

The menu scene is pretty quick to load, the only difference between the menu scene and the level1 scene is the amount of nodes that level one has, the menu scene and level 1 scene are sub classes of a game scene class.

Will try the fonts next,

Maybe its the way I'm loading the nodes?

import SpriteKit

class EncounterManager
{
    var encounterNames:[String] = []
    var currentEncounterIndex:Int = 0
    var previousEncounterIndex:Int = 0
    var encounters:[SKNode] = []
    var levelYpos:CGFloat = 60.0;


    func createNodes( encounterNames:[String])
    {
        self.encounterNames = encounterNames

        for encounterFileName in encounterNames
        {
            let encounter = SKNode()

            if let encounterScene = SKScene(fileNamed: encounterFileName) {

            for placeholder in encounterScene.children {

                let node = placeholder as SKNode

                switch node.name! {


                    // entities

                    case "Level-end":
                        let end = LevelEnd()
                        end.spawn(encounter, position:node.position, size:CGSize(width: 72, height: 33), useLeft:false);
                        break

                    case "Platform-left":
                        let platform = Platform()
                        platform.spawn(encounter, position:node.position, size:CGSize(width: 72, height: 33), useLeft:true);
                        break

                    case "Platform-right":
                        let platform = Platform()
                        platform.spawn(encounter, position:node.position, size:CGSize(width: 72, height: 33), useLeft:false);
                        break

                    case "Bat":
                        let bat = Bat()
                        bat.spawn(encounter, position: node.position)
                        break

                    case "Bee":
                        let bee = Bee()
                        bee.spawn(encounter, position: node.position)
                        break

It needs to be refactored the switch statement is pretty long.

Also Im getting, 60FPS with around 40 to 80 nodes - I don't use the simulator, only an iPhone 5

garyconstable
  • 309
  • 3
  • 15
  • It is not normal. It can happen because of a few things... And probably is something silly, but first things first. Do you preload your textures / atlases at all ? And do you preload sounds in any way ? – Whirlwind Apr 21 '16 at 14:59
  • I don't know about pre loading but the images do come from atlases. - Good call on the sounds, i'll take them out and see what happens. thanks – garyconstable Apr 21 '16 at 16:32
  • I preloaded all of the atlases, have seen at least a 10% increase in available memory - its the CPU usage I need to get down, tested on a scene with less nodes, it loaded much quicker. – garyconstable Apr 25 '16 at 12:27
  • When I present a new scene, do i have to kill off the old scene - or do anything, could this be using up CPU? – garyconstable Apr 25 '16 at 12:32
  • Nope, the old scene is replaced with the new one, once the transition is over. It may happen that old one is not released though (override scene's deinit to check this). So if sounds & music is not the issue, and you say that you have [correctly preloaded textures](http://stackoverflow.com/a/29421625/3402095), then what is your nodes count and draws count ? Also, are you testing on a device or Simulator ? – Whirlwind Apr 25 '16 at 12:45
  • And the silly part, I've mentioned before http://stackoverflow.com/a/23624123/3402095 . I doubt this is your issue, but a good thing to keep in mind. – Whirlwind Apr 25 '16 at 12:46

0 Answers0