OnBack() problem

Monkey Targets Forums/Android/OnBack() problem

jayparker(Posted 2014) [#1]
To my next problem :)

I have this code in OnBack() method. If user presses the back button on android, I want to move one screen back. But this won't work. Something missing??
For example current APP_STATE = STATE_GAME and when I hit the back button, I get to the STATE_CHOOSE_CAMPAIGN instead of STATE_CHOOSE_MISSION. Any suggestions?

	Method OnBack()
	
		If APP_STATE=STATE_TITLE
			Error("")
		ElseIf APP_STATE=STATE_CHOOSE_CAMPAIGN
			cpf_zone.Enable(1,3)
			cpf_zone.Disable(20,24)
			SetState STATE_TITLE
		ElseIf APP_STATE=STATE_CHOOSE_MISSION			
			cpf_zone.Enable(12)
			Old_x=mox
			Old_y=moy
			Stage_Moving=0
			Stage_Button_dx=Stage_Button_X[0]
			SetState STATE_CHOOSE_CAMPAIGN
		ElseIf APP_STATE=STATE_GAME
			cpf_zone.Enable(20,24)
			cpf_zone.Disable(50,55)
			SetState STATE_CHOOSE_MISSION
		EndIf
			
	End Method





skid(Posted 2014) [#2]
Looking at Android target source there is no OnBack, instead your game should receive a KEY_BACK key down key up pair.


Midimaster(Posted 2014) [#3]
I often use OnBack() this way on Android. And it works.

When the user press the BACK button during the game it returns to the main screen. When the user presses BACK during the main Screen, the app ends.
Method OnBack%()
	If ScreenMode=START_MENU 
		EndApp()
	Else
		ScreenMode=START_MENU
	Endif
	Return 0
End	



In your case it sound like "pressing twice" the BACK button. In this case it would look like returning direct to the STATE_TITLE. Add some PRINT commands to clearify, what really happens in succession.


jayparker(Posted 2014) [#4]
"pressign twice" was exactly the problem. I have OnBack() method and each screen has If KeyHit(KEY_ESCAPE). I removed the KeyHit command and now it works!


skid(Posted 2014) [#5]
Oops, I need to update, sorry for misinformation.


Wylaryzel(Posted 2014) [#6]
Looking at the code, I would suggest a SELECT CASE loop for easier code reading:

Method OnBack()
	
		Select APP_STATE
                         Case STATE_TITLE
			          Error("")
		         Case STATE_CHOOSE_CAMPAIGN
			          cpf_zone.Enable(1,3)
			          cpf_zone.Disable(20,24)
			          SetState STATE_TITLE
		          Case STATE_CHOOSE_MISSION			
			cpf_zone.Enable(12)
			Old_x=mox
			Old_y=moy
			Stage_Moving=0
			Stage_Button_dx=Stage_Button_X[0]
			SetState STATE_CHOOSE_CAMPAIGN
		Case STATE_GAME
			cpf_zone.Enable(20,24)
			cpf_zone.Disable(50,55)
			SetState STATE_CHOOSE_MISSION
		End
			
	End Method


But only a small suggestion - as you prefer