#ifndef __TRANSITIONSCENE_H__
#define __TRANSITIONSCENE_H__ #include "GameFrameHead.h" class CGameScene; class GAMEFRAME_API CSplashScene : public CCScene
{
public:
enum TargetScenes
{
TargetSceneINVALID = ,
TargetSceneFirstScene = ,
TargetSceneOtherScene = ,
TargetSceneMAX = ,
};
public:
CSplashScene();
virtual ~CSplashScene(); static CSplashScene* create(CGameScene* pScene);
bool init();
bool init(CGameScene* pScene); void retainPrevResource();
protected:
virtual void onEnter();
virtual void onEnterTransitionDidFinish();
void loadingCallBack(CCObject *obj); void onDelayLoading();
void replaceScene(); virtual void loadingPercent();
float getPercent();
private:
//virtual void update(float dt); private:
static CSplashScene* g_pSplashScene; CGameScene* m_pCurScene;
int m_nCurTag;
CGameScene* m_pNextScene; int m_nNumberOfSprites;
int m_nNumberOfLoadedSprites; float m_fPercent; CCLabelTTF* m_pLabelLoading; bool m_bRetainResource; }; #endif //__TRANSITIONSCENE_H__
#include "SplashScene.h"
#include "Config.h"
#include "XSprite.h"
#include "GameScene.h" CSplashScene* CSplashScene::g_pSplashScene = NULL; CSplashScene::CSplashScene()
{
m_pCurScene = NULL;
m_pNextScene = NULL;
m_nNumberOfSprites = ;
m_nNumberOfLoadedSprites = ;
m_fPercent = ;
m_bRetainResource = false;
} CSplashScene::~CSplashScene()
{
CCLog("CSplashScene::~CSplashScene");
} CSplashScene* CSplashScene::create(CGameScene* pScene )
{ CSplashScene* pSplashScene = new CSplashScene();
if (pSplashScene && pSplashScene->init(pScene))
{
pSplashScene->autorelease();
return pSplashScene;
} CCLog("error: CTransitScene::create");
SAFE_DELETE(pSplashScene);
return NULL; } bool CSplashScene::init( CGameScene* pScene )
{
m_pCurScene =(CGameScene*) CCDirector::sharedDirector()->getRunningScene();
if (m_pCurScene)
{
m_nCurTag = m_pCurScene->getSceneTag();
} m_pNextScene = pScene; return true;
} void CSplashScene::onEnter()
{
CCScene::onEnter(); } void CSplashScene::loadingCallBack( CCObject *obj )
{
++m_nNumberOfLoadedSprites;//每调用一次就+1
m_fPercent = (float) m_nNumberOfLoadedSprites / (float)m_nNumberOfSprites;
this->loadingPercent(); //全部加载完成后,就执行
if (m_nNumberOfLoadedSprites == m_nNumberOfSprites)
{
//加载完 延迟一下切换场景
CCDelayTime* pActDelay = CCDelayTime::create(0.5f);
CCFiniteTimeAction* pAction = CCSequence::create(pActDelay,
CCCallFunc::create(this, callfunc_selector(CSplashScene::replaceScene)),
NULL);
this->runAction(pAction);
} } void CSplashScene::onEnterTransitionDidFinish()
{
CCScene::onEnterTransitionDidFinish(); if (!m_bRetainResource)
{
//CCTextureCache::sharedTextureCache()->removeAllTextures();
//释放当前一个场景
if (CConfig::getInstance()->getElementInfoByItem(m_nCurTag))
{
vector<PlistInfo>& vecCurPlist = *(CConfig::getInstance()->getElementInfoByItem(m_nCurTag));
for (vector<PlistInfo>::iterator it = vecCurPlist.begin(); it != vecCurPlist.end(); it++)
{
CCSpriteFrameCache::sharedSpriteFrameCache()->removeSpriteFramesFromFile(it->strPlist.c_str());
}
}
} CCDelayTime *delayAction = CCDelayTime::create(0.2f);
CCCallFunc *callSelectorAction = CCCallFunc::create(this,callfunc_selector(CSplashScene::onDelayLoading));
this->runAction(CCSequence::create(delayAction, callSelectorAction,NULL)); } void CSplashScene::onDelayLoading()
{
//加载下一个场景
//初始化元素 把plist形式的图片资源加载到内存
vector<PlistInfo>& vecNextPlist = *(CConfig::getInstance()->getElementInfoByItem(m_pNextScene->getSceneTag()));
m_nNumberOfSprites = vecNextPlist.size();
for (vector<PlistInfo>::iterator it = vecNextPlist.begin(); it != vecNextPlist.end(); it++)
{
//加载图片到内存
CCSpriteFrameCache::sharedSpriteFrameCache()->addSpriteFramesWithFile(it->strPlist.c_str(),it->strImage.c_str());
} CCDelayTime *delayAction = CCDelayTime::create(0.2f);
CCCallFunc *callSelectorAction = CCCallFunc::create(this,callfunc_selector(CSplashScene::replaceScene));
this->runAction(CCSequence::create(delayAction, callSelectorAction,NULL));
} void CSplashScene::replaceScene()
{
CCDirector::sharedDirector()->replaceScene(CCTransitionFade::create(1.2f, (CCScene*)m_pNextScene));
} void CSplashScene::loadingPercent()
{ } float CSplashScene::getPercent()
{
return m_fPercent;
} void CSplashScene::retainPrevResource()
{
m_bRetainResource = true;
}