Newbie Question - How to quit an app?

Monkey Archive Forums/Monkey Discussion/Newbie Question - How to quit an app?

Arabia(Posted 2013) [#1]
Hi guys.

Made my first post yesterday (Android) section, and since then I've moved from "Hello World" into writing Naughts & Crosses with little to no help from looking at too many examples - which I'm pretty happy with at this stage since I've never written anything for any of these target platforms ever.

Okay, so my fabulous game works, however, I do have a button (area of the screen) to touch/click to end the game but I can't work out how to end the application. Can anyone give me a hint?

I haven't read all of the tutorial stuff yet, just want to get this finished so that I can impress the hell out of my niece & nephew tomorrow with this masterpiece of a game :)

Just bought Monkey Game Development from Packt - looking forward to having a read through that and learning lots.

So anyway, Hi to everyone that I haven't already spoken to on the Blitz Forums, you'll probably see a fair few more simple/complex questions in the coming days & weeks.


Xaron(Posted 2013) [#2]
EndApp()


Beside that you don't have to quit mobile apps. Actually for iOS you MAY not quit it.


Arabia(Posted 2013) [#3]
Thanks Xaron. I had a feeling that was the case.

When I put EndApp() in my code, I get a Identifier 'EndApp' not found error.

To be completely honest, I have very little experience in even using tablets let alone writing programs for them.

So to exit an app as such, it's just a matter of pressing the button on the tablet (not even sure what that button is called?) to go back to the main tablet screen.

Oh, and if a mod wants to move this topic, feel free - I meant to post this in the Monkey Programming section but put it here by mistake.


Xaron(Posted 2013) [#4]
Have you an "Import mojo" at the top of your app?

Better is to use the OnSuspend and OnResume methods, as the smartphone OS usually will handle the life time of your app.

I one had an exit button in one apps of mine and it was rejected by Apple due to exactly this reason.


Arabia(Posted 2013) [#5]
Yeah I've got an Import mojo at the top of the app.

I have zero idea about OnSuspend & OnResume, haven't even looked at what these do..... yet.

Still trying to get my head around all of this App stuff - I'm thinking like a Windows programmer and that probably isn't the best thing to do, this is a whole new target I'm writing for.

I guess the way I should be thinking of it is this - if I play a Flash game using an internet browser, I don't "quit" from the game, I just close my browser or the Tab it is open in - guessing games on a Tablet are sort of the same?

I did see somewhere in the help files, mention of "savestate" which I'm guessing is what is used to store the state of the game when a user stops playing and is reloaded when they start playing again.

Anyway, I'll have a read of the eBook I purchased, I'm sure it will answer a million of my questions.

Thanks for your help.


Xaron(Posted 2013) [#6]
You should have that basic construct:

Strict

Import mojo

Function Main:Int()
  New MyGame()
  Return 0
End Function

Class MyGame Extends App

  'Called once, do your initialization here!
  Method OnCreate:Int()
    SetUpdateRate(30)
    Return 0
  End Method

  'Called 30 times per second (normally)
  Method OnUpdate:Int()
    Return 0
  End Method
  
  'This is called when the user pressed the home button!
  Method OnSuspend:Int()
    Return 0
  End Method
  
  'This is called when the user switches back to your app and it's still alive!
  Method OnResume:Int()
    Return 0
  End Method

  'This is called when something is rendered to the screen
  Method OnRender:Int()
    Cls()
    DrawText( "Hello there", 10, 10 )
    Return 0
  End Method
  
  'This is called when the back button was pressed!
  Method OnBack:Int()
    'Ok, just exit the app for test purposes!
    EndApp()
    Return 0
  End Method
End Class



Xaron(Posted 2013) [#7]
I guess the way I should be thinking of it is this - if I play a Flash game using an internet browser, I don't "quit" from the game, I just close my browser or the Tab it is open in - guessing games on a Tablet are sort of the same?


Basically, as you come from a Windows background..

OnSuspend is called when the window of your app is switched to the background (and not updated anymore).
OnResume is called when the user presses Alt-Tab to switch back to your application.

On a smartphone OnSuspend is called when the user presses the Home button. In that case your app is still in the memory but sleeping. As soon as the OS decides that it needs some memory, your app is removed by the OS. That's why you normally never have to close an app on mobile devices! As I told Apple even reject apps who do that!
OnResume is called when the user switches back to your app and it's still alive (if it's not alive, OnCreate is called again)!
OnCreate is called once during the startup (only if it's NOT alive anymore)
OnUpdate is called x times per second (set this with SetUpdateRate in OnCreate)
OnRender is called to draw everything
OnBack is called when the user presses the back button (obviously not available on iOS devices but Android and Windows Phone)

Please note that EndApp() does NOT work for the HTML5 and/or similar targets.

All in all you have an event based application here. So you don't have a game loop which runs indefinitely. You just have OnUpdate and OnRender called frequently.


Arabia(Posted 2013) [#8]
Wow. Didn't know any of this. Thanks very much for the info.

I'll hopefully have a bit more of a play tomorrow and maybe write something more than X's & O's :)


MikeHart(Posted 2013) [#9]
I recommend to really study every edge of Monkeys documentation. A lot, if not everything is written down there.


Gerry Quinn(Posted 2013) [#10]
If you want an Exit? (Yes/No) on Android apps, take a look at diddy.externfunctions.ShowAlertDialog(). You can modify this so it doesn't ask for a string, and sets a value for 'Yes'.