cocos2dx3.3开发FlappyBird总结八:载入场景LoadingScene

载入场景的目的是预加载资源,也就是在场景进入时,把资源加载到内存中:

// 重写onEnter方法,场景载入时,会调用此方法,此外我们还需要调一下父类的方法,这个是API说明的,照做就行。
// 方法其实功能是很简单的,就是先显示一张splash图片,然后异步加载图片资源,这个addImageAsync方法是引擎内部提供的API,可异步加载,这样就不会阻塞主线程了。
void LoadingScene::onEnter() {
  Layer::onEnter();

  // Add the splash screen image
  auto background = Sprite::create("splash.png");
  auto size = Director::getInstance()->getVisibleSize();
  auto origin = Director::getInstance()->getVisibleOrigin();

  background->setPosition(origin.x + size.width / 2, origin.y + size.height / 2);
  this->addChild(background);

  // Start to load texture async
  auto texture = Director::getInstance()->getTextureCache();
  texture->addImageAsync("atlas.png",
                         CC_CALLBACK_1(LoadingScene::onLoadFinishedCallback, this));
}

加载完成后,就是加载音频文件,这里是同步加载的,如果资源很多的话,会卡的。加载完成后,就进入到WelcomeScene了。

void LoadingScene::onLoadFinishedCallback(cocos2d::Texture2D *texture) {
  // Load the atlas
  AtlasLoader::getInstance()->loadAtlas("atlas.txt", texture);

  // Preload effect music.
  // Here is not the best way to load all music files,
  // in real projects, you should find a better way to do.
  CocosDenshion::SimpleAudioEngine::getInstance()->preloadEffect("sfx_die.ogg");
  CocosDenshion::SimpleAudioEngine::getInstance()->preloadEffect("sfx_hit.ogg");
  CocosDenshion::SimpleAudioEngine::getInstance()->preloadEffect("sfx_point.ogg");
  CocosDenshion::SimpleAudioEngine::getInstance()->preloadEffect("sfx_swooshing.ogg");
  CocosDenshion::SimpleAudioEngine::getInstance()->preloadEffect("sfx_wing.ogg");

  // Now the task of the loading scene has finished,
  // auto enter into the welcome scene
  auto scene = TransitionFade::create(1.0f, WelcomeScene::createScene());
  Director::getInstance()->replaceScene(scene);
}

下一步:说说欢迎场景

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值