Hello,
I want to implement the back button delegate function but I am stuck at:
bool CCApplication::onBackButton() { bool ff = true; if (m_pDelegate) ff=m_pDelegate->onBackPressed(false); return ff; }
In reply to this Pink Nibor said:
I was not able to find where to place those lines. But, I found a working solution, it is probably not the best but it works :
I changed the OnBackButtonPressed function to call the keyBackClicked function of the current scene (CCLayer child of CCScene). Since I only use single layer in my scene, it works for me. But I think you can make it work even if you have multiple layers.
@
void CCFrameworkView::OnBackButtonPressed(Object^ sender, BackPressedEventArgs^ args)
hey Muhammad Wajahat:
thank you for you reply!
I want to know what’s the “m_bRunning” in you code and if i need to add the variable in every layer ?
Muhammad Wajahat wrote:
I was not able to find where to place those lines. But, I found a working solution, it is probably not the best but it works :
>
I changed the OnBackButtonPressed function to call the keyBackClicked function of the current scene (CCLayer child of CCScene). Since I only use single layer in my scene, it works for me. But I think you can make it work even if you have multiple layers.
@
void CCFrameworkView::OnBackButtonPressed(Object^ sender, BackPressedEventArgs^ args)
>
{
/* CCLog(“Came into OnBackButtonPressed”);
int c=CCDirector::sharedDirector()>getRunningScene>getChildrenCount;
char buf ;
_itoa_s;
CCLog;/
CCArray layers=CCDirector::sharedDirector~~>getRunningScene~~>getChildren;
if {
layers~~>objectAtIndex)>keyBackClicked;
//if m_BackDelegate>onBackPressed;
// bool ff = CCApplication::sharedApplication.onBackButton;
// args~~>Handled = ff;
args~~>Handled=layers~~>objectAtIndex(0))>m_bRunning;
} else args>Handled=true;
}@
Oh that is just a variable of CCLayer class. As I said it is not a good way of doing things, it is kind of a hack. If args->Handled is false after this function call then application closes. And therefore, I set m_bRunning=false, only in the MenuScene (first page)’s keyBackClicked function. By default the m_bRunning for any layer is true as far as I know when it returns.
So I get: Close the app if back button pressed on first page (_scene)
Otherwise only follow the logic in keyBackClicked function.
Remember: The app is closed when the program counter (control) returns from the keyBackClicked function for the menuscene as well.
It just typo.
Here is my solution and it works for sure!
In class (Helloworld for example) .h: define:
void onEnter();
void onExit();
In class .cpp:
void HelloWorld::onEnter()
{
CCApplication::sharedApplication().setBackDelegate(this);
}
void HelloWorld::onExit()
{
CCApplication::sharedApplication().setBackDelegate(NULL);
}
The typo here is instead of using “.” he used “->” lol