0

So im developing a very simple game. Basically I will be using Swift + Firebase.

Users can login in to my game and click a button to fight a "boss". But other users can also fight the same boss "instance".

On my Firebase DB there will be a reference to this "instance" of the "boss" with a certain amount of "life","dmg to players per second", "# of players fighting it".

I will use the Realtime Database to update the "Boss's Life" and "# of Players Fighting it" on each user's ViewController.

My question is since the "Boss instance" does "damage per second" how can i make sure thats running even when users put the game in background?

I can put NSTimer in viewWillAppear()but I want the player to be damaged even in background and if the player's life reaches 0 -> to save that in DB and note that "player has died" in the DB as well. So when they come back it will run a check and tell them they have died and boot them back to the Main View Controller.

Hope this makes sense!

Iv been trying to figure this out for weeks and well now im at a point where i cant skip it!

brkr
  • 1,208
  • 1
  • 12
  • 18
  • there is no way to reliably run an nstimer in the background in iOS. you may have to put users in the backend if you want to do this – Jared Jun 01 '16 at 23:00
  • @Jared hmm Is there a way I can show a UIAlert when they click the "home" button or before they try to go away from my app to warn them if they leave they will be "considered dead"? – brkr Jun 01 '16 at 23:04
  • 1
    hmm. I am not sure if you can show an alertview, but in your app delegate there is a method called applicationWillResignActive where you can run code before the app goes to the background. edit: yep you definitely cannot show an alert view in that method. maybe warn the user right before they fight the boss? – Jared Jun 01 '16 at 23:09
  • hmm so If i use `applicationWillResignActive` i can use setValue on the Firebase DB where the user is located and note "user died"? Does `aplicationWillResignActive` execute only when they leave the app (Home button) or actually close the app? or both? – brkr Jun 01 '16 at 23:13
  • 1
    there is applicationDidEnterBackground, resignActive, and applicationWillTerminate. You can handle each of these cases differently, or make a method and call it in all these cases. Here are some good docs about each state: https://developer.apple.com/library/ios/documentation/iPhone/Conceptual/iPhoneOSProgrammingGuide/TheAppLifeCycle/TheAppLifeCycle.html – Jared Jun 01 '16 at 23:16
  • Ok awesome thanks man! Do you think the way im building it is decent? Just curious what kind of backend language could I use to make this easier? I havnt really learned anything besides Swift and Firebase for all my apps. I dont really understand how the backend server will keep "dmging the players" while they are offline or app is closed.. – brkr Jun 01 '16 at 23:18
  • also note that making database calls in these cases is not consistent with how apple intends them to be used. Look into beginBackgroundTaskWithExpirationHandler for the database calls – Jared Jun 01 '16 at 23:18
  • But im not using CoreData im using Firebase Realtime Database to update.. would they not accept this app? – brkr Jun 01 '16 at 23:19
  • 1
    check this answer out and youll be fine: http://stackoverflow.com/questions/10319643/objective-c-proper-use-of-beginbackgroundtaskwithexpirationhandler – Jared Jun 01 '16 at 23:21
  • 1
    if you want to do this in another language I would check out Ruby on Rails or PHP. But what youre doing is fine and those would add unnecessary complication so keep it up! – Jared Jun 01 '16 at 23:22
  • Ok thanks :) Any thoughts on this? http://stackoverflow.com/questions/37599113/how-to-structure-game-to-add-more-items-to-unlock-later-on – brkr Jun 02 '16 at 18:03

0 Answers0