I'm using cocos2d, and I want to know how I can detect what scene the user was last on before he/she pressed a button to go开发者_StackOverflow中文版 to my Settings scene from my pauseLayer scene. I need to know because there is 2+ ways to access the Settings scene, so I would need a special back button to go back to whatever scene the user was last at. Also, I could have 1 back button that has a method to go back to the last scene, not any specific one. How would I go about doing this?
Edit: Based on your code sample, I suggest the following, which may not work exactly as written due to my lack of testing equipment :P.
First, add a method to your Settings class called +(id)nodeFromSource:(bool)bPauseMenu.
+(id)nodeFromSource:(bool)bPauseMenu
{
if((self = [self node])
{
m_bPauseMenu = bPauseMenu;
}
return self;
}
Add bool m_bPauseMenu; in the class definition.
In the function you assign for your back button in Settings, implement the following code:
//for example...
-(void)backButtonPressed:(id)sender
{
if(m_bPauseMenu)
{
[[CCDirector sharedDirector] replaceScene:[CCTransitionFlipAngular transitionWithDuration:1.2f scene:[pauseLayer node]]];
}
else
{
//trigger a replaceScene back to the other menu here
//(MyOtherLayer is the classname of your non-pause-menu layer that you came from...
[[CCDirector sharedDirector] replaceScene:[CCTransitionFlipAngular transitionWithDuration:1.2f scene:[MyOtherLayer node]]];
}
}
Change the call in pauseLayer -(void)settings:(id)sender to:
[[CCDirector sharedDirector] replaceScene:[CCTransitionFlipAngular transitionWithDuration:1.2f scene:[Settings nodeFromSource:true]]];
And wherever your other call to show the Settings layer is, make it say:
[[CCDirector sharedDirector] replaceScene:[CCTransitionFlipAngular transitionWithDuration:1.2f scene:[Settings nodeFromSource:false]]];
The reason I called it nodeFromSource is so that one day you can replace the bool with some kind of reference and allow your layers to know things about the layers they came from.
I hope this works as-is. I've been over it a few times and can't see any obvious errors...
精彩评论