- 本帖最后由 googleman 于 2012-11-21 07:12 编辑 *
大家好!
我是一只小小鸟!各位老鸟好!有一个在android平台的“基本”问题想象各位老鸟请救!描述如下:
我使用一个layer制作了一个MODAL对话框。当用户从对话框中作出各种选择后,系统作出相应的响应。我的对话框类如下:
class GameoverDlg : public CCLayer {
private:
// 模态对话框菜单(containing two buttons)
CCMenu *m_pMenu;
// 记录菜单点击
bool m_bTouchedMenu;
…
bool init(){
if (!CCLayer::init())
{
return false;
}
this->setTouchEnabled(true);
this->setKeypadEnabled(true);
return true;
}
void keyBackClicked()
{
this->removeFromParentAndCleanup(true);
}
void MenuItemCallback(cocos2d::CCObject *pSender)
{
CCDirector::sharedDirector()->sharedDirector()->getRunningScene()->removeChild(this, true);
SoundManager::playPressBtn();
// get the userdata, it's the index of the menu item clicked
CCMenuItem* pMenuItem = (CCMenuItem *)(pSender);
int nIdx = pMenuItem->getZOrder() - 10000;
switch(nIdx){
case 0:
{................
this->removeFromParentAndCleanup(true);
CCScene *pScene = FirstInterface::scene();
CCTransitionScene *transition = CCTransitionZoomFlipAngular::create(1.0f,pScene, kOrientationRightOver);
CCDirector::sharedDirector()->replaceScene(transition);
}
break;
case 1:
{
this->removeFromParentAndCleanup(true);
//Not save and exit
CCUserDefault::sharedUserDefault()->setBoolForKey("bStored", false);
CCScene *pScene = FirstInterface::scene();
CCTransitionScene *transition = CCTransitionZoomFlipAngular::create(1.0f,pScene, kOrientationRightOver);
CCDirector::sharedDirector()->replaceScene(transition);
}
break;
case 2:
this->removeFromParentAndCleanup(true);
break;
}
}
void onEnter()
{
CCLayer::onEnter();
CCDirector::sharedDirector()->getTouchDispatcher()->addTargetedDelegate(this, kCCMenuHandlerPriority - 1, true);
…
}
void onExit()
{
CCLayer::onExit();
…
CCDirector::sharedDirector()->getTouchDispatcher()->removeDelegate(this);
}
bool ccTouchBegan(cocos2d::CCTouch *pTouch, cocos2d::CCEvent *pEvent)
{
m_bTouchedMenu = m_pMenu->ccTouchBegan(pTouch, pEvent);
return true;
}
void ccTouchMoved(cocos2d::CCTouch *pTouch, cocos2d::CCEvent *pEvent)
{
if (m_bTouchedMenu) {
m_pMenu->ccTouchMoved(pTouch, pEvent);
}
}
void ccTouchEnded(cocos2d::CCTouch *pTouch, cocos2d::CCEvent *pEvent)
{
if (m_bTouchedMenu) {
m_pMenu->ccTouchEnded(pTouch, pEvent);
m_bTouchedMenu = false;
}
}
void ccTouchCancelled(cocos2d::CCTouch *pTouch, cocos2d::CCEvent *pEvent)
{
if (m_bTouchedMenu) {
m_pMenu->ccTouchEnded(pTouch, pEvent);
m_bTouchedMenu = false;
}
}
};
在我的游戏主类中,我使用了如下方式调用上面的LAYER:
void GameLayer::showGameoverDlg()
{
SoundManager::playPopupDlg();
CCSize s = CCDirector::sharedDirector()->getWinSize();
CCSprite* backSprite = CCSprite::create(“images/gamelayer/dialog_bg.png”);//???
float layerWidth =backSprite->boundingBox().size.width;
float layerHeight =backSprite->boundingBox().size.height;
GameoverDlg* l =new GameoverDlg(this,layerWidth,layerHeight, backSprite);
l->setPosition((s.width - layerWidth) / 2,(s.height - layerHeight) / 2);
l->autorelease();
CCDirector::sharedDirector()->sharedDirector()->getRunningScene()->addChild(l,88888);
}
释放资源的代码大致如下:
void GameLayer::onExit()
{
CCLayerColor::onExit();
…
SimpleAudioEngine::sharedEngine()->end();
CCDirector::sharedDirector()->purgeCachedData();
}
现在的问题是:当我点击MODAL中的“保存并退出”或者“不保存并退出”,然后通过上面的replaceScene返回到前一个scene(FirstInterface)时,前一个场景FirstInterface界面只是简单地闪烁一下,便出现系统CRASH。
补充:我从前一个场景FirstInterface切换到上面的游戏主场景GameLayer时感觉比较正常,使用了如下代码:
void FirstInterface::startButtonHandler(CCObject* sender)
{
SimpleAudioEngine::sharedEngine()->playEffect(std::string(CCFileUtils::sharedFileUtils()->fullPathFromRelativePath(EFFECT_FILE)).c_str());
CCScene *pScene = GameLayer::scene();
CCTransitionScene *transition = CCTransitionZoomFlipAngular::create(1.0f,pScene, kOrientationRightOver);
CCDirector::sharedDirector()->replaceScene(transition);
}
怎么往回切换(由GameLayer到FirstInterface)就变得那么复杂了呢?是否是释放GameLayer中数据不当导致的吗?
请问各位老鸟,我上面的设计思路是否存在问题?我这几天都快闷死了!HELP!!!