How to delay the start of a function - swift

so i have built my first application in X Code using swift, and i have also taken the time to add the game centre functionality to it, however my question is, how would i delay the start of my game, in order for game centre to load, as of right now, the game starts, and then the game centre banner appears on top of my game before disappearing.
would it be sufficient to use something like, on the function containing my main game attributes:
NSTimer.scheduledTimerWithTimeInterval()
or
would i be better off adding a splash screen?
if anyone has any other suggestions, that would be helpful.
thanks in advance.

Better to subscribe under GameCenter, listen a complete event and then produce your next initialization. Avoid timers in similar situations, because time of processing depends from many factors, and rely to specific delay is not good idea.

If you are waiting for the game to load, then starting a timer is probably not the best solution, because you don't know how long the loading takes on different devices with different capabilities.
A better idea is to load your game and then have a completion method that is called to start the game when it is ready to be run.

Related

Creating a general purpose timer for execution of events and updating variables within a SpriteKit GameScene

I'm building a game in SpriteKit and while I've seen various posts about making timers for executing run actions like MoveTo. But I wanted to know the best way to go about building a general purpose timer for the whole GameScene. Something that could be referenced almost globally, or passed by value to functions as to tell them when to execute. For example, as of now I spawn a boss on intervals of 50 killed enemies but I want to be able to reference this timer to spawn one, say every minute. I could have a variable at the top of GameScene that is updated in Update, but I'm not sure if this makes sense long term (especially with pausing or general reference). Any advice would be great.
Timer implementation using SKAction may be of use for almost every aspect in a game:
spawning enemies, updating time left labels, regenerating health, regenerating shields or whatever can be regenerated and so on...
There is an update: method with its passed currentTime parameter, where you can do whatever you can achieve using SKActions. It is up to you what you are going to use.
About using global timers. I don't really get what you are referring to and where you can have a need for them... Timers like described above, are often defined per scene, because usually, you don't have to count how much time is passed while transitioning between scenes. If you have to, then, we will think of something :) But, that is rare and I will skip that situation.
All of these timers, when game is paused, should be paused as well. You don't want to see that you have run out of time after a return from a phone call, right? Or you don't want to see full screen of enemies (which both would likely happen if you use NSTimer for time related actions). So as you can see, there is an NSTimer, but I would skip a story about it, because there are lot of posts, here on SO, where people debating about should we use it in SpriteKit or should we skip it. I would say, just skip it because you don't need it at all. So using SKAction or update: method will allow you to have everything paused when game is interrupted (actions will be paused automatically).
On the other hand, implementing timers for a specific purpose, say implementing refilling lives feature, is not a task that SKActions (nor some update: method implementation) can solve. That is because you have to calculate how much time have passed since a certain moment, but a user can terminate an app in a meanwhile. In this case, using something like NSUserDefaults will give you a way to solve app termination, so you can continue counting from where you have left of next time user starts the app. But there is a catch... A user can mess with his clock, so you don't want to rely on client time, and this is usually solved using a server and its time. I guess this may be what you called "a global timer".

How to handle iPhone 'Low Battery' pop up during a game

I was wondering how low battery alerts can be handled during a game. I used Cocos2D to make my game. I have the game pause when an interrupt such as a text or a call come in. But what about low battery alerts? Are they the same as other interrupts? It does not seem to be.
Is there any way I can detect when it comes up so that I can pause the game?
Thanks for your replies.
You can set up "battery monitoring" and get an event when this happens. Some links with info:
http://mobileorchard.com/new-in-iphone-30-tutorial-series-part-5-battery-monitoring
http://mobiledevelopertips.com/device/display-battery-state-and-level-of-charge.html
You know the event you're worried about is when it hits 20%, so I think you can just check for that amount and pause when it happens.
This is perhaps not a perfect solution, as Apple could decide it should happen at 25%, not 20%.
Also, Apple recommends turning this on only when you really need to know. So it should only be activated during gameplay, not on menus that aren't time sensitive.
Thanks guys. The problem however lied elsewhere. It happened to be a problem with one of the flags im checking upon resume. It should get reset when a user starts a new game, which I failed to do.
Lesson learnt: Be extra careful with global variables :) [I try to avoid them as much as possible]
I appreciate your replies and your answers. Thanks again.

Lag when authenticating local user on Game Center

For my cocos2d game, I authenticate the local user in the applicationDidFinishLaunching method of my AppDelegate. However, whenever the authentication is complete, it will cause a short lag in my game when the little "Welcome back, X" message slides down and back up. The problem is I have no control over when this authentication is complete – obviously the duration is highly dependent on the data connection of the device.
Sometimes the message (and the un-avoidable accompanying lag) appears as soon as when I am in the menu scene, which is somewhat acceptable, since my menu is more or less static. More often than not, it happens later, when the game has already started. Because my game is an endless scroller, the message always causes a lag in the movement of the player, even causing the player to die sometimes (half a second of lag is enough :-/).
How might I circumvent this? I used to have a loading scene right before my menu scene to load some of my assets, and because the loading takes a while, there was always a good chance that the authentication is completed at the loading scene, but of course I can't guarantee that it will be true!
Game Center can make the game freeze for a little as its starts if your connection isn't great. I doubt that loading GameCenter on a different thread would be a good idea (or even have an effect on that) but what you could do as a work around of the issue you are having is to give the game a 3-2-1 countdown before it starts scrolling/moving. would give a little more time for game center and time for the player to build his focus, starting serious in a game straight away might not be to the liking of some players.
I hope this helps.

mixing OpenGL and Interface Builder/ UI Controls - bad idea? Why? (iPhone)

I've heard that OpenGL ES and standard iPhone UI controls don't play well together, but I'm wondering if anyone knows why, and what the effects are? I'm writing an OpenGL based game, and the view is loaded from a nib file with ui controls, and it seems to work ok, but the game is really simple at this point... does using ui controls cause some kind of performance hit?
UI events momentarily pause timers, like when scrolling a tableview. You can get around this by using the common runtime mode when creating a timer. It may slow down your rendering if you have a lot of layers because they all need to get redrawn every-time you refresh. So if your game runs at 60fps it will also redraw everything on top of the GLView, like UIImageViews, buttons etc. 60 times a second, which is a huge waste. It might not make a huge impact on your frame rate but it may make the device run hotter and drain the battery faster. Its best to draw your HUD using OpenGL, but it depends on the situation. For something that will be displayed only for a short time, like a menu I think you can get away with it.
Theres nothing wrong with it, its just wasteful.

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