Monday, August 4, 2014

[English] Ansyc load the texture

In loading scene you should use ansyc load the atlas and other resource.

void LoadingScene::onEnter(){
    // add background to current scene
    Sprite *background = Sprite::create("splash.png");
    Size visibleSize = Director::getInstance()->getVisibleSize();
    Point origin = Director::getInstance()->getVisibleOrigin();
    background->setPosition(origin.x + visibleSize.width/2, origin.y + visibleSize.height/2);
    this->addChild(background);

    // start ansyc method load the atlas.png
    Director::getInstance()->getTextureCache()->addImageAsync("GameAtlas.png", CC_CALLBACK_1(LoadingScene::loadingCallBack, this));
}


void LoadingScene::loadingCallBack(Texture2D *texture){
    
    auto plPath = FileUtils::getInstance()->fullPathForFilename("GameAtlas.plist");
    //auto plPath = "atlas/GameAtlas.plist";
    auto frameCache = SpriteFrameCache::getInstance();
    frameCache->addSpriteFramesWithFile(plPath, texture);

    // After loading the texture , preload all the sound
    SimpleAudioEngine::getInstance()->preloadEffect("audio1.ogg");

    // After load all the things, change the scene to new one
    auto scene = MainScene::create();
    TransitionScene *transition = TransitionFade::create(1, scene);
    Director::getInstance()->replaceScene(transition);
}


No comments:

Post a Comment