Diddy - v.70d - OnBack - OnClose?

Monkey Forums/Monkey Programming/Diddy - v.70d - OnBack - OnClose?

Amon(Posted 2013) [#1]
How do I handle this now?

Basically pressing the back button on the Samsung Galaxy S4 shuts the app down immediately. Apologies if it is simple to do but an example would be appreciated; navigating from screen to screen etc!

Ta!


Amon(Posted 2013) [#2]
Well, I ended up RTFM and have sorted it. :)


Amon(Posted 2013) [#3]
For anyone else wondering how to fix it:

In the Main class that extends DiddyApp put:

	Method OnBack:Int()
		If currentScreen = titleScreen
			EndApp()
		EndIf
		If currentScreen = gameScreen
			gameScreen.FadeToScreen(titleScreen)
		EndIf
		Return 0
	End



Samah(Posted 2013) [#4]
I've just added OnBack support to Diddy such that it has the same exception-handling as the other On<Event> methods, and calls Screen.Back().

By default, OnBack() does nothing (preventing your app from closing on the back button). To make it do something, you will need to set the screen.backScreenName field to the name of whichever screen to want to fade to. Setting it to "exit" will end the program.

Global fooScreen:FooScreen = New FooScreen
Global barScreen:BarScreen = New BarScreen

Class FooScreen Extends Screen
	Method New()
		name = "foo"
		backScreenName = "exit"
	End
	
	Method Render:Void()
	End
	
	Method Update:Void()
		If KeyHit(KEY_ESCAPE) Then
			Self.Back()
		ElseIf KeyHit(KEY_SPACE) Then
			FadeToScreen(barScreen)
		End
	End
End

Class BarScreen Extends Screen
	Method New()
		name = "bar"
		backScreenName = "foo"
	End
	
	Method Render:Void()
	End
	
	Method Update:Void()
		If KeyHit(KEY_ESCAPE) Then Self.Back()
	End
End

If your game catches the escape key to fade to the previous screen, you may want to change it to call Self.Back() instead. This will give your escape key the same functionality as pressing the back button.