1
votes

I have a standard UIViewController (TestViewController), and in this controller's viewWillLayoutSubviews method I instantiate a new SKView subclass (TestView) and add it as a subview to the controller's view. In addition I add some buttons which are meant to let the user control actions in an SKScene subclass (TestScene). TestScene is instantiated in an init method on TestView. Immediately after TestView is added to TestViewController's view as a subview I also call [testView presentScene] which is a method on the TestView that simply calls [self presentScene: testScene].

Pressing the mentioned buttons on TestViewController is meant to call a method on TestScene (via TestView) that will instantiate a new SKSpriteNode subclass (TestSpriteNode) and add it as a child to the scene. This does work, but my problem is that for the first TestSpriteNode to get added there is intermittently a long delay in the TestScene rendering the new sprite. From time to time it works perfectly but most of the time it will get stuck and after multiple touches of the button multiple sprites will appear all at once, after which everything functions perfectly. I should mention that this problem occurs on the simulator as well as on my device.

I have tried adding a new TestSpriteNode in the TestScene's -(void)didMoveToView:(SKView*)view resulting in the TestSpriteNode appearing immediately and then there being a delay when the button is pressed to add a new TestSpriteNode. By adding a break point to the method that the button press calls on TestScene I can see that it is called immediately but the added TestSpriteNode is not rendered immediately.

I apologise in advance if there is already an answer that addresses delays in rendering dynamically added SKSpriteNodes, I was not able to find it. Is there something that I have misunderstood about adding new SKSpriteNodes to an SKScene?

1
I found the solution to my problem here link. Making new SKScenes for different screens rather than new UIViewControllers fixes the problemBokoskokos

1 Answers

0
votes

Without code it's hard to say. But consider this: if the sprite uses a texture that hasn't been loaded into memory, creating the sprite will load the image file as a texture. The larger the image the longer it takes to load it. This will not happen for subsequent sprites using the same texture.

If you aren't already make sure all images are added to an SKTextureAtlas, and preload the texture atlas before or while creating the scene. The atlas will then allow you to get textures by image name and use them to create sprites without any texture-loading delays.