Cocos2d-X 愤怒的小鸟实现----加载页面(一)
1、头文件中
#include
#include "cocos2d.h"
#include "Scence.h"
#include "DefineHeader.h"
#include "StartLayer.h"
#include "SimpleAudioEngine.h"
#include "ConstDefine.h"
using namespace CocosDenshion;
USING_NS_CC;
class LoadingScene:public CCLayer
{
public:
virtual bool init();
void BackImage();
CREATE_FUNC(LoadingScene);
SCENE_FUNC(LoadingScene);
int count;
int assetCount;
virtual void onEnter();
virtual void onExit();
virtual void onEnterTransitionDidFinish();
void GameClick();
void loadSpriteSheets(CCArray *SpriteSheet);
void loadAssets(CCArray *Assets);
void loadImages(CCArray *Images);
void loadSoundFX(CCArray *SoundFX);
void loadMusic(CCArray *musicFiles);
void SourceUpdate();
bool isLoading;
float progressInterval;
CCProgressTimer *progress;
void loadingCompleted();
void ImageCompleteClick();
}; 2、实现文件中
bool LoadingScene::init()
{
if( !CCLayer::init() )
{
return false;
}
this->BackImage();//加载背景头片
//加载资源的过程
CCSpriteFrameCache::sharedSpriteFrameCache()->addSpriteFramesWithFile("preloadAssetManifest.plist");
CCDictionary *dic=CCDictionary::createWithContentsOfFile("preloadAssetManifest.plist");
CCArray *spriteSheets=(CCArray *)dic->objectForKey("SpriteSheets");
CCArray *Assets=(CCArray *)dic->objectForKey("Assets");
CCArray *Images=(CCArray *)dic->objectForKey("Images");
CCArray *SoundFX=(CCArray *)dic->objectForKey("SoundFX");
//资源的总数
assetCount=Assets->count()+spriteSheets->count()+Images->count()+SoundFX->count();
if(spriteSheets)
{
this->loadSpriteSheets(spriteSheets);
}
if(Assets)
{
this->loadAssets(Assets);
}
if(Images)
{
this->loadImages(Images);
}
if(SoundFX)
{
this->loadSoundFX(SoundFX);
}
progressInterval = 100/(float)assetCount;
this->schedule(schedule_selector(LoadingScene::SourceUpdate),2);
isLoading=true;
count=0;
return true;
}
//加载精灵到视图
void LoadingScene::BackImage()
{
//加载背景图
CCSprite *spLoad=CCSprite::create(JiaZaiZiyuan);
spLoad->setPosition( ccp(WINSIZE.width/2,WINSIZE.height/2));
this->addChild(spLoad);
//添加加载的进度条
progress = CCProgressTimer::create(CCSprite::create(PROGRESSBAR));
//设置进度条的比例
progress -> setBarChangeRate(ccp(1,1));
progress -> setPosition(ccp(WINSIZE.width/2,WINSIZE.height/6));
progress -> setType(kCCProgressTimerTypeBar);
progress -> setPercentage(0.0);
progress -> setScale(0.6);
this->addChild(progress);
//添加标签
CCLabelTTF *loadingLabel = CCLabelTTF::create("Loading", "Arial", 25);
//loadingLabel->setPosition(ccpSub(progress->getPosition(), ccp(0, 30)));
loadingLabel->setPosition(ccp(120, 30));
loadingLabel->setAnchorPoint(ccp(0, 0.5));
loadingLabel->setTag(1);
loadingLabel -> setColor(ccc3(249, 2, 207));
this->addChild(loadingLabel);
}
void LoadingScene::onEnter()
{
CCLayer::onEnter();
}
void LoadingScene::onExit()
{
CCLayer::onExit();
this->unscheduleAllSelectors();
this->unschedule(schedule_selector(LoadingScene::SourceUpdate));
}
void LoadingScene::onEnterTransitionDidFinish()
{
CCLayer::onEnterTransitionDidFinish();
//this->schedule(schedule_selector( LoadingScene::GameClick), 0.5f);
}
//加载。。。。。。游戏中
void LoadingScene::GameClick()
{
CCLabelTTF *label=(CCLabelTTF *)this->getChildByTag(1);
const char* name=label->getString();
char str[100];
sprintf(str, "%s.", name);
label->setString(str);
if(count++>5)
{
CCTransitionSlideInL *slide=CCTransitionSlideInL::create(1, StartLayer::scene());
CCDirector::sharedDirector()->replaceScene(slide);
}
}
//加载spriteSheets
void LoadingScene::loadSpriteSheets(CCArray *SpriteSheet)
{
//加载图片用这个方法
// CCTextureCache::sharedTextureCache()->addImageAsync("", this, callfuncN_selector(<#_SELECTOR#>));
for(int i=0;icount();i++)
{
const char *spriteFile=((CCString *)SpriteSheet->objectAtIndex(i))->m_sString.c_str();
CCSpriteFrameCache::sharedSpriteFrameCache()->addSpriteFramesWithFile(spriteFile);
--assetCount;
}
}
//加载Assets
void LoadingScene::loadAssets(CCArray *Assets)
{
//加载图片用这个方法
// CCTextureCache::sharedTextureCache()->addImageAsync("", this, callfuncN_selector(<#_SELECTOR#>));
for(int i=0;icount();i++)
{
--assetCount;
}
}
//加载Images
void LoadingScene::loadImages(CCArray *Images)
{
//加载图片用这个方法
// CCTextureCache::sharedTextureCache()->addImageAsync("", this, callfuncN_selector(<#_SELECTOR#>));
for (int i=0; icount(); i++)
{
const char *spriteFile = ((CCString *)Images->objectAtIndex(i))->m_sString.c_str();
// CCTextureCache::sharedTextureCache()->addImage(spriteFile);
CCTextureCache::sharedTextureCache()->addImageAsync(spriteFile, this, callfuncO_selector(LoadingScene::ImageCompleteClick));
--assetCount;
}
}
//每一帧图片加载完成后所执行的事件
void LoadingScene::ImageCompleteClick()
{
CCLog("jia zai wan cheng");
}
//加载SoundFX(音效)
void LoadingScene::loadSoundFX(CCArray *SoundFX)
{
//加载图片用这个方法
// CCTextureCache::sharedTextureCache()->addImageAsync("", this, callfuncN_selector(<#_SELECTOR#>));
for(int i=0;icount();i++)
{
const char *soundFX=((CCString *)SoundFX->objectAtIndex(i))->m_sString.c_str();
//CCSpriteFrameCache::sharedSpriteFrameCache()->addSpriteFramesWithFile(soundFX);
SimpleAudioEngine::sharedEngine()->preloadBackgroundMusic(soundFX);
--assetCount;
}
}
//加载Sound(音乐)
void LoadingScene::loadMusic(CCArray *musicFiles)
{
//加载图片用这个方法
// CCTextureCache::sharedTextureCache()->addImageAsync("", this, callfuncN_selector(<#_SELECTOR#>));
for(int i=0;icount();i++)
{
const char *musics=((CCString *)musicFiles->objectAtIndex(i))->m_sString.c_str();
// CCSpriteFrameCache::sharedSpriteFrameCache()->addSpriteFramesWithFile(musics);
SimpleAudioEngine::sharedEngine()->preloadBackgroundMusic(musics);
--assetCount;
}
}
void LoadingScene::SourceUpdate()
{
// progressInterval = 100/(float)assetCount;
if (isLoading)
{
progress ->setPercentage(100-progressInterval*assetCount);
if (progress->getPercentage()<100)
{
CCProgressTo *to = CCProgressTo::create(1, progress->getPercentage());
progress->runAction(to);
}
else
{
isLoading = false;
CCProgressFromTo *end = CCProgressFromTo::create(0.3, progress->getPercentage(), 100);
this->schedule(schedule_selector(LoadingScene::GameClick),0.5);
CCSequence *sequence = CCSequence::create(end,NULL);
progress -> runAction(sequence);
}
}
}
void LoadingScene::loadingCompleted()
{
CCTransitionSlideInL *slide=CCTransitionSlideInL::create(1, StartLayer::scene());
CCDirector::sharedDirector()->replaceScene(slide);
}
版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。
暂时没有评论,来抢沙发吧~