Load scene in background while displaying animation - swift

I've seen a lot of questions like this, none of which work because they're in an old version of Swift (all the dispatch_global stuff).
My game currently freezes for several seconds before switching scenes. Any way to have an animation while the next scene loads?

Related

How To Implement Loading Screen

I was wondering how can I implement a loading screen in my game. I have seen it in many popular games and I would like to add one. It's not just for fun but I have seen some slow loading times between scenes and would just like to add a smooth touch to the user experience instead of them thinking that their iDevice crashed.
You can use SKTransition for that:
var transition = SKTransition.fadeWithDuration(5)
self.view?.presentScene(yourScene, transition: transition)
This code fades out your old scene and smoothly loads the new one.
There are many different options available you could use instead of fadeWithDuration:
crossFadeWithDuration(_:)
doorsCloseHorizontalWithDuration(_:)
doorsCloseVerticalWithDuration(_:)
doorsOpenHorizontalWithDuration(_:)
doorsOpenVerticalWithDuration(_:)
doorwayWithDuration(_:)
fadeWithColor(_:duration:)
flipHorizontalWithDuration(_:)
flipVerticalWithDuration(_:)
moveInWithDirection(_:duration:)
pushWithDirection(_:duration:)
revealWithDirection(_:duration:)
init(CIFilter:duration:)

iOS game lagging after loading view frequently

I am developping a relatively simple frame based iphone game, and it runs quite smoothly. However, when I switch views and then back again a couple of times everything starts to lagg severely.
Maybe someone recognizes this as a simple programming problem. (e.g. not setting iVars to nil, not switching views correctly, loading too much variables at the same time, etc.)
NOTE: this happens only after having switched views over 10 times.

Question about CCSpriteSheet

I'm using cocos2d to develop game,but i 've got some problems.
In game the first time collided with the props would to cause the program slow down,after that
the program runs smoothly,but the position of props in screen wouldn't match with the position in my code(such as overlap)
I 've checked the console,it show :
CCSpriteSheet: resizing TextureAtlas capacity from [1] to [2].
I guess that may be the point to cause program slow down,but i did not use the CCSpriteSheet anywhere. That confused me a lot....
Does anyone encountered the same situation?or give me some suggestions?
thanks a lot ~~~
Only slows down the first time it runs? I worked around this by running all of my actions or things which happen (in your case this collision) so that they all happened together on a loading screen.
This means that all the actions which initially slow down my game are cleared up behind the scenes as it were.
So what I did:
When the scene first runs, add a splash loading screen (simply an image hiding your scene) run all your actions while this loading screen can be seen, then once they have al been run reset everything and remove loading screen. Its pretty easy.
This way when the player actually plays the game it runs smoothly because the initial loading has already happened.

iPhone Opengl game with ads == fps problem?

I have a game that runs fine as is (around 30fps), but fps went down the drain when I tried to implement ads. I tried Greystripe and iAds but with same result (iAds were maybe bit worse). Average fps is almost same, but there are huge spikes all the times (1-2 spikes per second) and game is unplayable.
I guess it is because ad is in another view. I read somewhere that opengl apps on iphone don't like having another views with them, but there is plenty of games with ads on app store. How do they do it?
My implementation should be ok. I did everything as documentation and samples told me. I have my opengl view and ad view as subviews in app window, adview being in front of opengl view and thus covering part of it. Could this be the problem? Is it better to make opengl view smaller to left space for ad so they don't overlap? Do you have any other ideas what could be wrong?
Lope, I've created a gist at this link with a singleton "AdManager" class I wrote to handle iAds using cocos2d. Cocos2d sits on top of OpenGL, of course, and I've found that this code doesn't affect FPS even for relatively complicated games.
You'll have to modify this a bit to work with your application, changing out the cocos2d calls, etc, but this will give you asynchronous loading of iAds, which should help the FPS issue.
To use this class, include its header and call
[[AdManager sharedManager] attachAdToView:self.view];
wherever you need iAds. The ads will remain hidden until an ad loads, at which time they'll pop up at the top of screen. (The class works for iOS 4.0, 4.1 and 4.2).
Also, I should add that I have cocos2d running inside of an overall UIViewController that I call "Cocos2DController". When I attach the ads to a cocos2d view, I'm using
[[AdManager sharedManager] attachAdToView:[[CCDirector sharedDirector] openGLView]];
Best of luck!
We can hit and miss with apple's choices, but go for the sure thing and implement the ads in other parts to be appealing and not intrusive. It will be better for the framerate, and for you.
Try downloading the ads in a seperate, low priority, thread. You can, thus, nsure that the ads loading does not take too much CPU time. With a bit of CPU synchronisation you can make sure you don't try to display the new ad until it is completely ready to display. Sure it will suck some CPU time away from what you are trying to do but set your priorities right and it should only suck time when you are busy doing nothing.
Please excuse the thread necro'ing here, but I've used Stack Overflow a lot to help me through the problems I've had during coding, and thought my experience might be useful to someone in the future.
My simple cocos2d game ran with decent FPS (rarely changed the FPS display at all) until I implemented AdWhirl (integrating AdMob + iAd only). It would then run OK for the first few iterations, but upon upon the 9th or 10th scene refresh (single screen game, time in each scene < 5 seconds on average) the FPS would dive to ~20FPS, and drop again each time the scene refreshed.
Turns out, in my n00biness (this may be particular to me :) ), I was calling the scene from within itself. That is, once the actions had finished, the last action was to call the main scene again (a lazy way of rebuilding the scene for the user to have another go). This init'd the views and view controllers I had inserted to handle the AdWhirl ads all over again, and not only did I have a memory leak, I had 10+ view controllers all trying to request and service ads from AdWhirl. Once I got a clue and took that self-referring loop out, all was good.

Freezes application in button action

I doing a game application using cocos2d.In this application two game are include together. When playing game i include a action for switching the game. when i use this button two or three times change the game but after that the game is freezes & not work any more until the game is close the game. If anyone have a solution than give me some information that how can i overcome in this problem.
Are you sure you don't have memory leak problems when switching games?
Its difficult to diagnose correctly with so little information but my guess would be that the switch process isnt done correctly and after a few times some memory/variables get corrupted